-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinatorics.cpp
More file actions
54 lines (49 loc) · 1.08 KB
/
Combinatorics.cpp
File metadata and controls
54 lines (49 loc) · 1.08 KB
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
#include <bits/stdc++.h>
using namespace std;
// firstly import Modular Code
struct Comb {
int n;
vector<Int> _fact, _invfact, _inv;
Comb() : n{0}, _fact{1}, _invfact{1}, _inv{0} {};
Comb(int m) : Comb() {
f(m);
}
void f(int m) {
if (m <= n) return;
_fact.resize(m + 1);
_invfact.resize(m + 1);
_inv.resize(m + 1);
for (int i = n + 1; i <= m; i++) {
_fact[i] = _fact[i - 1] * i;
_inv[i] = modInverse(Int(i));
_invfact[i] = _invfact[i - 1] * _inv[i];
}
n = m;
}
Int fact(int m) {
f(m);
return _fact[m];
}
Int invfact(int m) {
f(m);
return _invfact[m];
}
Int inv(int m) {
f(m);
return _inv[m];
}
Int C(int N, int K) {
if (K < 0 || K > N) return 0;
f(N);
return _fact[N] * _invfact[K] * _invfact[N - K];
}
Int P(int N, int K) {
if (K < 0 || K > N) return 0;
f(N);
return _fact[N] * _invfact[N - K];
}
} comb;
int main()
{
return 0;
}