-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmint.cpp
More file actions
84 lines (65 loc) · 1.71 KB
/
mint.cpp
File metadata and controls
84 lines (65 loc) · 1.71 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
typedef long long ll;
constexpr int MAX_N = 1e6 + 14, MOD = 1'000'000'007;
struct Mint {
int x;
Mint(ll x = 0) : x((x % MOD + MOD) % MOD) {
}
explicit operator int() const {
return x;
}
bool operator==(const Mint& rhs) const {
return x == rhs.x;
}
bool operator!=(const Mint& rhs) const {
return !(rhs == *this);
}
friend Mint operator+(const Mint& l, const Mint& r) {
return l.x + r.x;
}
Mint& operator+=(const Mint& o) {
return *this = *this + o;
}
friend Mint operator-(const Mint& l, const Mint& r) {
return l.x - r.x;
}
Mint operator-() const {
return -x;
}
Mint& operator-=(const Mint& o) {
return *this = *this - o;
}
friend Mint operator*(const Mint& l, const Mint& r) {
return (ll)l.x * r.x;
}
Mint& operator*=(const Mint& o) {
return *this = *this * o;
}
Mint pow(ll b) const {
Mint ans = 1;
Mint a = *this;
for (; b; b >>= 1, a = a * a)
if (b & 1)
ans *= a;
return ans;
}
friend Mint operator/(const Mint& l, const Mint& r) {
return l * r.pow(MOD - 2);
}
Mint& operator/=(const Mint& o) {
return *this = *this / o;
}
friend ostream& operator<<(ostream& os, const Mint& o) {
return os << o.x;
}
};
Mint fac[MAX_N] = {1}, rfac[MAX_N] = {1};
void prep() {
for (int i = 1; i < MAX_N; ++i)
fac[i] = fac[i - 1] * i;
rfac[MAX_N - 1] = 1 / fac[MAX_N - 1];
for (int i = MAX_N - 2; i >= 0; --i)
rfac[i] = (i + 1) * rfac[i + 1];
}
Mint c(int n, int r) {
return n < r || r < 0 ? 0 : fac[n] * rfac[r] * rfac[n - r];
}