forked from anshulkataria007/HactoberFest-2020-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnCr.cpp
56 lines (46 loc) · 926 Bytes
/
nCr.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
/*Use ll with this algorithm */
vector<int> fact(1e6);
int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
int modInverse(int n, int p){
return power(n, p-2, p);
}
void calFactorial(int mod){
fact[0] = 1;
int n = fact.size();
for(int i = 1; i < n; i ++){
fact[i] = (fact[i-1]*i)%mod;
}
}
int nCr(int n, int r)
{
if (r > n)
return 0;
int dr = modInverse(fact[r], mod);
int dnr = modInverse(fact[n - r], mod);
int ans = ((fact[n] % mod) * (dr % mod)) % mod;
ans = (ans * (dnr % mod))%mod;
return ans % mod;
}
int32_t main(){
int n,r;
cin>>n>>r;
calFactorial(mod);
cout << nCr(n, r) << endl;
return 0;
}