forked from mateusvrs/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer-mod.cpp
66 lines (60 loc) · 1.86 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
const ll INF = 1e18;
const ll mod = 998244353;
template <ll MOD = mod>
struct Modular {
ll value;
static const ll MOD_value = MOD;
Modular(ll v = 0) {
value = v % MOD;
if (value < 0) value += MOD;
}
Modular(ll a, ll b) : value(0) {
*this += a;
*this /= b;
}
Modular& operator+=(Modular const& b) {
value += b.value;
if (value >= MOD) value -= MOD;
return *this;
}
Modular& operator-=(Modular const& b) {
value -= b.value;
if (value < 0) value += MOD;
return *this;
}
Modular& operator*=(Modular const& b) {
value = (ll)value * b.value % MOD;
return *this;
}
friend Modular mexp(Modular a, ll e) {
Modular res = 1;
while (e) {
if (e & 1) res *= a;
a *= a;
e >>= 1;
}
return res;
}
friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
Modular operator++(int) { return this->value = (this->value + 1) % MOD; }
Modular operator++() { return this->value = (this->value + 1) % MOD; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
Modular operator--(int) {
return this->value = (this->value - 1 + MOD) % MOD;
}
Modular operator--() { return this->value = (this->value - 1 + MOD) % MOD; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {
return os << a.value;
}
friend bool operator==(Modular const& a, Modular const& b) {
return a.value == b.value;
}
friend bool operator!=(Modular const& a, Modular const& b) {
return a.value != b.value;
}
};