-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnCr.cpp
More file actions
34 lines (33 loc) · 930 Bytes
/
nCr.cpp
File metadata and controls
34 lines (33 loc) · 930 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
struct nCr{
ll maxx , md;
vll fact, ifact;
inline ll mul(ll a, ll b) { return a *1LL* b % md ;}
ll power(ll a, ll n) {
if(n == 0) return 1 ;
int p = power(a, n/2) % md ;
p = mul(p, p) ;
return n & 1 ? mul(p, a) : p ;
}
int invMod(int a) {return power(a,md-2);}
void pre() {
fact[0] = 1 ;
for(int i = 1;i< maxx;++i) fact[i] = mul(i, fact[i-1]) ;
ifact[maxx-1] = invMod(fact[maxx-1]) ;
for(int i = maxx-1 ; i>0 ;--i) ifact[i-1] = mul(ifact[i], i) ;
}
nCr(int _mxN, int _M) {
maxx = _mxN + 1;
md = _M ;
fact.resize(maxx) ;
ifact.resize(maxx) ;
pre() ;
}
ll C(ll n, ll r) {
if (n < r || r < 0 || n < 0) return 0;
return mul(fact[n], mul(ifact[r], ifact[n-r])) ;
}
};
//maxx N we need
//const int N = 100;
// initialise nCr struct
// nCr comb(N , mod);