-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModInt.code-snippets
114 lines (114 loc) · 4.84 KB
/
ModInt.code-snippets
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
105
106
107
108
109
110
111
112
113
114
{
"ModInt": {
"prefix": "ModInt",
"body": [
"template < int MOD = 1000000007 > struct ModInt {",
"",
" int val;",
"",
" ModInt(int V = 0) : val(V) { val %= MOD; }",
"",
" ModInt& operator += (const ModInt& rhs) {",
" if ((val += rhs.val) >= MOD) val -= MOD;",
" return *this;",
" }",
" ModInt& operator += (const int rhs) {",
" if ((val += rhs) >= MOD) val -= MOD;",
" return *this;",
" }",
"",
" ModInt& operator -= (const ModInt& rhs) { ",
" if ((val += MOD - rhs.val) >= MOD) val -= MOD; ",
" return *this; ",
" }",
" ModInt& operator -= (const int rhs) { ",
" if ((val += MOD - rhs) >= MOD) val -= MOD; ",
" return *this; ",
" }",
"",
" ModInt& operator *= (const ModInt& rhs) { val = (1ll * val * rhs.val) % MOD; return *this; }",
" ModInt& operator *= (const int rhs) { val = (1ll * val * rhs) % MOD; return *this; }",
"",
" ModInt& operator /= (const ModInt& rhs) { return *this *= rhs.inverse(); }",
" ModInt& operator /= (const int rhs) { return *this *= ModInt(rhs).inverse(); }",
"",
" ModInt& operator %= (const ModInt& rhs) { val %= rhs.val; return *this; }",
" ModInt& operator %= (const int rhs) { val %= rhs; return *this; }",
"",
" ModInt& operator ++() { return *this += 1; }",
" ModInt& operator --() { return *this -= 1; }",
" ",
" ModInt operator ++(int unused) { ModInt res(*this); ++*this; return res; }",
" ModInt operator --(int unused) { ModInt res(*this); --*this; return res; }",
" ",
" ModInt operator + (const ModInt& rhs) const { ModInt res(*this); return res += rhs; }",
" ModInt operator + (const int rhs) const { ModInt res(*this); return res += rhs; }",
"",
" ModInt operator % (const ModInt& rhs) const { ModInt res(*this); return res %= rhs; }",
" ModInt operator % (const int rhs) const { ModInt res(*this); return res %= rhs; }",
"",
" ModInt operator - (const ModInt& rhs) const { ModInt res(*this); return res -= rhs; }",
" ModInt operator - (const int rhs) const { ModInt res(*this); return res -= rhs; }",
"",
" ModInt operator * (const ModInt& rhs) const { ModInt res(*this); return res *= rhs; }",
" ModInt operator * (const int rhs) const { ModInt res(*this); return res *= rhs; }",
"",
" ModInt operator / (const ModInt& rhs) const { ModInt res(*this); return res /= rhs; }",
" ModInt operator / (const int rhs) const { ModInt res(*this); return res /= rhs; }",
"",
" ModInt& operator = (const ModInt& rhs) { val = rhs.val; return *this; }",
" ModInt& operator = (const int rhs) { val = rhs; return *this; }",
"",
" bool operator == (const ModInt& rhs) const { return val == rhs.val; }",
" bool operator == (const int rhs) const { return val == rhs; }",
"",
" bool operator != (const ModInt& rhs) const { return val != rhs.val; }",
" bool operator != (const int rhs) const { return val != rhs; }",
"",
" bool operator < (const ModInt& rhs) const { return val < rhs.val; }",
" bool operator < (const int rhs) const { return val < rhs; }",
"",
" bool operator <= (const ModInt& rhs) const { return val <= rhs.val; }",
" bool operator <= (const int rhs) const { return val <= rhs; }",
"",
" bool operator > (const ModInt& rhs) const { return val > rhs.val; }",
" bool operator > (const int rhs) const { return val > rhs; }",
"",
" bool operator >= (const ModInt& rhs) const { return val >= rhs.val; }",
" bool operator >= (const int rhs) const { return val >= rhs; }",
"",
" int operator () () const { return val; }",
"",
" ModInt inverse() const { return power(MOD - 2); }",
"",
" ModInt power(ll n) const {",
" ModInt a = *this, res = 1;",
" while (n > 0) {",
" if (n & 1) res *= a;",
" a *= a, n >>= 1;",
" }",
" return res;",
" }",
"",
" ModInt power(ModInt n) const {",
" ModInt a = *this, res = 1;",
" int e = n();",
" while (e > 0) {",
" if (e & 1) res *= a;",
" a *= a, e >>= 1;",
" }",
" return res;",
" }",
"",
" friend ModInt operator ^ (ModInt rhs, ll n) { return rhs.power(n); }",
" friend ModInt operator ^ (ModInt rhs, ModInt n) { return rhs.power(n); }",
"",
" friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }",
" friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }",
"",
"};",
"using Mint = ModInt < 998244353 >;"
],
"description": "ModInt"
}
}