forked from sauravjoshi23/Competitive_Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modular.cpp
67 lines (62 loc) · 1.07 KB
/
modular.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
57
58
59
60
61
62
63
64
65
66
vector<int> fact, invfact, two;
int modpow(int x, int y, int p = mod)
{
int res = 1;
x %= p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
int add(int x, int y)
{
x += y;
if (x >= mod) return x - mod;
return x;
}
int sub(int x, int y)
{
x -= y;
if (x < 0) return x + mod;
return x;
}
int inv(int a, int m = mod)
{
return modpow(a, m - 2, m);
}
int mul(int a, int b)
{
return (long long) a * b % mod;
}
void calc_fact(int n)
{
fact.resize(n);
invfact.resize(n);
two.resize(n);
fact[0] = invfact[0] = two[0] = 1;
for(int i = 1; i < n; ++ i)
{
fact[i] = mul(fact[i - 1], i);
invfact[i] = inv(fact[i]);
two[i] = mul(two[i - 1], 2);
}
}
int ncr(int n, int r)
{
assert(n >= r && n >= 0 && r >= 0);
int ans = mul(fact[n], mul(invfact[n - r], invfact[r]));
return ans;
}
vector<vector<int>> choose(M + 1, vector<int>(M + 1));
for(int i = 0; i <= M; ++ i)
{
choose[i][0] = choose[i][i] = 1;
for(int j = 1; j < i; ++ j)
{
choose[i][j] = add(choose[i - 1][j - 1], choose[i - 1][j]);
}
}