forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modint.h
66 lines (49 loc) · 1.59 KB
/
modint.h
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
#include <bits/stdc++.h>
using namespace std;
template <int mod>
struct modint {
int value;
modint(long long x = 0) { value = normalize(x); }
int normalize(long long x) {
if (x < -mod || x >= mod)
x %= mod;
if (x < 0)
x += mod;
return static_cast<int>(x);
}
explicit operator int() const { return value; }
modint operator-() const { return modint(-value); }
modint &operator+=(modint rhs) {
if ((value += rhs.value) >= mod)
value -= mod;
return *this;
}
modint &operator-=(modint rhs) {
if ((value -= rhs.value) < 0)
value += mod;
return *this;
}
modint &operator*=(modint rhs) {
value = normalize(static_cast<long long>(value) * rhs.value);
return *this;
}
modint &operator/=(modint rhs) { return *this *= modint(inverse(rhs.value, mod)); }
int inverse(int a, int m) {
int u = 0, v = 1;
while (a != 0) {
int t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
bool operator==(modint rhs) const { return value == rhs.value; }
bool operator!=(modint rhs) const { return !(*this == rhs); }
friend modint operator+(modint lhs, modint rhs) { return lhs += rhs; }
friend modint operator-(modint lhs, modint rhs) { return lhs -= rhs; }
friend modint operator*(modint lhs, modint rhs) { return lhs *= rhs; }
friend modint operator/(modint lhs, modint rhs) { return lhs /= rhs; }
};