-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmint.cpp
More file actions
95 lines (78 loc) · 1.89 KB
/
mint.cpp
File metadata and controls
95 lines (78 loc) · 1.89 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
85
86
87
88
89
90
91
92
93
94
95
template <unsigned int mod = M>
struct mint {
unsigned int x;
mint() : x(0) {}
mint(long long _x) {
_x %= mod;
if (_x < 0) _x += mod;
x = _x;
}
mint& operator+=(const mint& a) {
x += a.x;
if (x >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint& a) {
x += mod - a.x;
if (x >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint& a) {
x = (long long)x * a.x % mod;
return *this;
}
mint pow(long long pw) const {
mint res = 1;
mint cur = *this;
while (pw) {
if (pw & 1) res *= cur;
cur *= cur;
pw >>= 1;
}
return res;
}
mint inv() const {
assert(x != 0);
unsigned int t = x;
unsigned int res = 1;
while (t != 1) {
unsigned int Mint = mod / t;
res = (long long)res * (mod - Mint) % mod;
t = mod - t * Mint;
}
return res;
}
mint& operator/=(const mint& a) {
return *this *= a.inv();
}
mint operator+(const mint& a) const {
return mint(*this) += a;
}
mint operator-(const mint& a) const {
return mint(*this) -= a;
}
mint operator*(const mint& a) const {
return mint(*this) *= a;
}
mint operator/(const mint& a) const {
return mint(*this) /= a;
}
bool operator==(const mint& a) const {
return x == a.x;
}
bool operator!=(const mint& a) const {
return x != a.x;
}
bool operator<(const mint& a) const {
return x < a.x;
}
friend ostream& operator<<(std::ostream& os, mint const& a) {
return os << a.x;
}
friend istream& operator>>(istream& is, mint& a) {
long long _x;
is >> _x;
a = mint(_x);
return is;
}
};