forked from kr-viku/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinomialCoefficient.cpp
46 lines (40 loc) · 913 Bytes
/
BinomialCoefficient.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
#include<bits/stdc++.h>
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long int
using namespace std;
int fact[1000001];
int mod = 1e9 + 7;
// Calculate C(n, k) % mod where mod > n
// formula is n! / k! * (n - k)! but here we are using mod and precalculating factorial
int power(int a, int n) {
int res = 1;
while(n) {
if(n & 1)
res = (res * a) % mod, n--;
else
a = (a * a) % mod, n /= 2;
}
return res;
}
int C(int n, int k) {
if(k > n)
return 0;
int res = fact[n];
// here instead of dividing we use inverse
res = (res * power(fact[k], mod - 2)) % mod;
res = (res * power(fact[n - k], mod - 2)) % mod;
return res;
}
int32_t main() {
int t, n, k;
cin >> t;
fact[0] = fact[1] = 1;
for(int i = 2; i <= 1000000; i++)
fact[i] = (fact[i - 1] * i) % mod;
while(t--) {
cin >> n >> k;
cout << C(n, k) << endl;
}
return 0;
}