-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcountings.h
47 lines (42 loc) · 1.28 KB
/
countings.h
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
class Countings{
private:
int mod;
vector<long long> factList, invList;
long long Pow(long long x, long long n);
public:
Countings(int sz, int mod);
long long Permutation(int n, int r);
long long Combination(int n, int r);
long long HomogeneousProduct(int n, int r);
};
Countings::Countings(int sz, int mod) : mod(mod), factList(sz + 1), invList(sz + 1) {
factList[0] = 1;
for(int i = 1; i < factList.size(); i++) {
factList[i] = factList[i - 1] * i % mod;
}
invList[sz] = Pow(factList[sz], mod - 2);
for(int i = sz - 1; i >= 0; i--) {
invList[i] = invList[i + 1] * (i + 1) % mod;
}
}
long long Countings::Pow(long long x, long long n){
long long ret = 1;
while(n > 0) {
if(n & 1) ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
long long Countings::Permutation(int n, int r){
if(r < 0 || n < r) return 0;
return factList[n] * invList[n - r] % mod;
}
long long Countings::Combination(int n, int r){
if(r < 0 || n < r) return 0;
return factList[n] * invList[r] % mod * invList[n - r] % mod;
}
long long Countings::HomogeneousProduct(int n, int r){
if(n < 0 || r < 0) return 0;
return (r == 0 ? 1 : Combination(n + r - 1, r));
}