forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbhaskar_nCr_mod_p.cpp
More file actions
41 lines (40 loc) · 892 Bytes
/
bhaskar_nCr_mod_p.cpp
File metadata and controls
41 lines (40 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define shazam ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define M 1000000007
#define pb(x) push_back(x)
#define m_p(x,y) make_pair(x,y)
ll gcd(ll a, ll b) {if (b == 0)return a; return gcd(b, a % b);}
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll modInverse(ll n, ll p)
{
return power(n, p - 2, p);
}
ll nCr(ll n, ll r, ll p)
{
ll ans = 1;
ll fact[n + 1];
fact[0] = 1;
for (ll i = 1; i <= n; i++)fact[i] = ((fact[i - 1] % p) * (i % p)) % p;
return (((fact[n] * modInverse(fact[n - r], p)) % p) * modInverse(fact[r], p)) % p;
}
int main()
{
shazam;
ll i, j, p, q, t, x, y, z, n, m, k, r;
cin >> n >> r >> p;
cout << nCr(n, r, p) << "\n";
}