-
Notifications
You must be signed in to change notification settings - Fork 0
/
modular.cpp
104 lines (83 loc) · 1.78 KB
/
modular.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000009;
ll Pow(ll a, ll b) {
ll x = 1, y = a;
while (b) {
if (b % 2) (x *= y) %= MOD;
(y *= y) %= MOD;
b /= 2;
}
return x;
}
ll Inv(ll x) {
return Pow(x, MOD - 2);
}
struct Mod {
ll x;
Mod () {}
Mod (ll _x) {
x = _x % MOD;
if (x < 0) x += MOD;
}
Mod operator+ (Mod rhs) {
ll ans = x + rhs.x;
if (ans >= MOD) ans -= MOD;
Mod ret; ret.x = ans; return ret;
}
Mod operator- (Mod rhs) {
ll ans = x - rhs.x;
if (ans < 0) ans += MOD;
Mod ret; ret.x = ans; return ret;
}
Mod operator* (Mod rhs) {
Mod ret; ret.x = x * rhs.x % MOD; return ret;
}
Mod operator+= (Mod rhs) {
x += rhs.x;
if (x >= MOD) x -= MOD;
return *this;
}
Mod operator-= (Mod rhs) {
x -= rhs.x;
if (x < 0) x += MOD;
return *this;
}
Mod operator*= (Mod rhs) {
x *= rhs.x;
x %= MOD;
return *this;
}
Mod operator^ (ll a) {
Mod ret; ret.x = Pow(x, a); return ret;
}
Mod operator^= (ll a) {
x = Pow(x, a);
return *this;
}
Mod inv() {
Mod ret; ret.x = Inv(x); return ret;
}
Mod operator/ (Mod rhs) {
return *this * rhs.inv();
}
Mod operator/= (Mod rhs) {
*this *= rhs.inv();
return *this;
}
};
Mod operator+ (ll a, Mod b) {
return Mod(a) + b;
}
Mod operator- (ll a, Mod b) {
return Mod(a) - b;
}
Mod operator* (ll a, Mod b) {
return Mod(a) * b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
srand((unsigned)chrono::high_resolution_clock::now().time_since_epoch().count());
}