-
Notifications
You must be signed in to change notification settings - Fork 2
/
Integer Mod.cpp
89 lines (84 loc) · 1.9 KB
/
Integer Mod.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
const ll MOD = 1 '000' 000'000 + 7;
template <ll _mod = MOD>
struct mint {
ll value;
static const ll MOD_value = _mod;
mint(ll v = 0) {
value = v % _mod;
if (value < 0) value += _mod;
}
mint(ll a, ll b) : value(0) {
*this += a;
*this /= b;
}
mint &operator+=(mint const &b) {
value += b.value;
if (value >= _mod) value -= _mod;
return *this;
}
mint &operator-=(mint const &b) {
value -= b.value;
if (value < 0) value += _mod;
return *this;
}
mint &operator*=(mint const &b) {
value = (ll)value * b.value % _mod;
return *this;
}
friend mint mexp(mint a, ll e) {
mint res = 1;
while (e) {
if (e & 1) res *= a;
a *= a;
e >>= 1;
}
return res;
}
friend mint inverse(mint a) {
return mexp(a, _mod - 2);
}
mint &operator/=(mint const &b) {
return *this *= inverse(b);
}
friend mint operator+(mint a, mint const b) {
return a += b;
}
mint operator++(int) {
return this->value = (this->value + 1) % _mod;
}
mint operator++() {
return this->value = (this->value + 1) % _mod;
}
friend mint operator-(mint a, mint const b) {
return a -= b;
}
friend mint operator-(mint const a) {
return 0 - a;
}
mint operator--(int) {
return this->value =
(this->value - 1 + _mod) % _mod;
}
mint operator--() {
return this->value =
(this->value - 1 + _mod) % _mod;
}
friend mint operator*(mint a, mint const b) {
return a *= b;
}
friend mint operator/(mint a, mint const b) {
return a /= b;
}
friend std::ostream &operator<<(
std::ostream &os, mint const &a) {
return os << a.value;
}
friend bool operator==(mint const &a,
mint const &b) {
return a.value == b.value;
}
friend bool operator!=(mint const &a,
mint const &b) {
return a.value != b.value;
}
};