-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod2.cpp
71 lines (69 loc) · 1.29 KB
/
mod2.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
67
68
69
70
71
const int mod = 1000000007;
struct mint {
long long x;
mint(long long x = 0):x((x % mod + mod) % mod) {}
mint operator-() const {return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
// 繰り返し二乗法で 2 の n 乗を計算する
mint f (int n) {
if (n == 0) {
return 1;
}
mint x = f(n / 2);
x *= x;
if (n % 2 == 1) {
x *= 2;
}
return x;
}
// a 回のループで計算する
mint choose (int n, int a) {
mint x = 1;
mint y = 1;
for (int i = 0; i < a; i++) {
x *= n - i;
y *= i + 1;
}
return x / y;
}