-
Notifications
You must be signed in to change notification settings - Fork 2
/
pnc.cpp
45 lines (43 loc) · 1004 Bytes
/
pnc.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
const int MOD = 1e9+7;
const int N = 2e5+5;
// Combnitorics Snippet, Call precompute()
int fact[N], ifact[N];
int mod_exp(int base, int exp, int mod = MOD)
{
int ans = 1;
while (exp) {
if(exp % 2) ans = ans % mod * base % mod;
base = (base % mod * base % mod) % mod;
exp /= 2; ans %= mod;
}
return ans;
}
int inv_mod(int base, int mod = MOD) {
return mod_exp(base, mod - 2, mod);
}
void add(int &a, int b) {
a += b;
if (a >= MOD)
a -= MOD;
}
void mul(int &a, int b) {
a *= b;
a %= MOD;
}
void pre_compute() {
fact[0] = 1;
for(int i = 1; i < N; i++) {
fact[i] = (i * fact[i-1]) % MOD;
}
ifact[N - 1] = inv_mod(fact[N - 1], MOD);
for (int i = N - 2; i >= 0; i--) {
ifact[i] = ((i + 1) * ifact[i + 1]) % MOD;
}
}
int nCr(int n, int r) {
if (r > n or r < 0)
return 0;
if(r == n or r == 0)
return 1;
return ( (fact[n] % MOD * ifact[r] % MOD) * ifact[n - r] ) % MOD;
}