-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodint.cpp
240 lines (215 loc) · 5.75 KB
/
modint.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// modint
//
// reference:
// drken: 「1000000007 で割ったあまり」の求め方を総特集! ~ 逆元から離散対数まで ~
// https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a
//
// verified:
// ABC 127 E - Cell Distance
// https://atcoder.jp/contests/abc127/tasks/abc127_e
//
#include <bits/stdc++.h>
using namespace std;
// modint
template<int MOD> struct Fp {
// inner value
long long val;
// constructor
constexpr Fp() : val(0) { }
constexpr Fp(long long v) : val(v % MOD) {
if (val < 0) val += MOD;
}
// getter
constexpr long long get() const {
return val;
}
constexpr int get_mod() const {
return MOD;
}
// comparison operators
constexpr bool operator == (const Fp &r) const {
return this->val == r.val;
}
constexpr bool operator != (const Fp &r) const {
return this->val != r.val;
}
// arithmetic operators
constexpr Fp& operator += (const Fp &r) {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp &r) {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp &r) {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp &r) {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b, swap(a, b);
u -= t * v, swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp operator + () const { return Fp(*this); }
constexpr Fp operator - () const { return Fp(0) - Fp(*this); }
constexpr Fp operator + (const Fp &r) const { return Fp(*this) += r; }
constexpr Fp operator - (const Fp &r) const { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp &r) const { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp &r) const { return Fp(*this) /= r; }
// other operators
constexpr Fp& operator ++ () {
++val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -- () {
if (val == 0) val += MOD;
--val;
return *this;
}
constexpr Fp operator ++ (int) {
Fp res = *this;
++*this;
return res;
}
constexpr Fp operator -- (int) {
Fp res = *this;
--*this;
return res;
}
friend constexpr istream& operator >> (istream &is, Fp<MOD> &x) {
is >> x.val;
x.val %= MOD;
if (x.val < 0) x.val += MOD;
return is;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD> &x) {
return os << x.val;
}
// other functions
constexpr Fp pow(long long n) const {
Fp res(1), mul(*this);
while (n > 0) {
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
constexpr Fp inv() const {
Fp res(1), div(*this);
return res / div;
}
friend constexpr Fp<MOD> pow(const Fp<MOD> &r, long long n) {
return r.pow(n);
}
friend constexpr Fp<MOD> inv(const Fp<MOD> &r) {
return r.inv();
}
};
// Binomial coefficient
template<class mint> struct BiCoef {
vector<mint> fact_, inv_, finv_;
constexpr BiCoef() {}
constexpr BiCoef(int n) : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
init(n);
}
constexpr void init(int n) {
fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
int MOD = fact_[0].get_mod();
for(int i = 2; i < n; i++){
fact_[i] = fact_[i-1] * i;
inv_[i] = -inv_[MOD%i] * (MOD/i);
finv_[i] = finv_[i-1] * inv_[i];
}
}
constexpr mint com(int n, int k) const {
if (n < k || n < 0 || k < 0) return 0;
return fact_[n] * finv_[k] * finv_[n-k];
}
constexpr mint fact(int n) const {
if (n < 0) return 0;
return fact_[n];
}
constexpr mint inv(int n) const {
if (n < 0) return 0;
return inv_[n];
}
constexpr mint finv(int n) const {
if (n < 0) return 0;
return finv_[n];
}
};
//------------------------------//
// Examples
//------------------------------//
void small_test() {
const int MOD = 998244353;
using mint = Fp<MOD>;
auto check = [&](mint val, mint res) -> void {
cout << val << endl;
assert(val == res);
};
// arithmetic operation
mint a = -3;
mint b = 5;
check(a + b, 2);
check(a - b, 998244345); // -8
check(a * b, 998244338); // -15
a += b;
check(a, 2);
a -= 3;
check(a, 998244352); // -1
a *= 6;
check(a, 998244347); // -6
a /= 2;
check(a, 998244350); // -3
// increment
check(++a, 998244351); // -2
check(a, 998244351); // -2
check(--a, 998244350); // -3
check(a, 998244350); // -3
check(b++, 5);
check(b, 6);
check(b--, 6);
check(b, 5);
// division
a = 6, b = 2;
check(a / b, 3);
mint c = b / a;
check(c, 332748118);
check(c * a, 2);
// pow, inv
check(b.pow(20), 1048576);
check(b.inv(), 499122177);
}
void ABC_127_E() {
const int MOD = 1000000007;
using mint = Fp<MOD>;
long long N, M, K;
cin >> N >> M >> K;
BiCoef<mint> bc(N * M);
mint sum = 0;
for (int i = 0; i <= N-1; ++i) {
for (int j = 0; j <= M-1; ++j) {
mint tmp = mint(N - i) * mint(M - j) * mint(i + j);
if (i != 0 && j != 0) tmp *= 2;
sum += tmp;
}
}
cout << sum * bc.com(N * M - 2, K - 2) << endl;
}
int main() {
small_test();
//ABC_127_E();
}