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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
namespace Integer {
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef long double ld;
namespace Math {
#define G 3
#define MOD1 998244353
#define MOD2 1004535809
#define M 1002772198720536577ll
#define M1 334257240187163831ll
#define M2 668514958533372747ll
#define lg(a) (31 - __builtin_clz(a))
#define inc1(a, b) (a + b >= MOD1 ? a + b - MOD1 : a + b)
#define inc2(a, b) (a + b >= MOD2 ? a + b - MOD2 : a + b)
#define dec1(a, b) (a < b ? a - b + MOD1 : a - b)
#define dec2(a, b) (a < b ? a - b + MOD2 : a - b)
#define mul1(a, b) (1ll * a * b % MOD1)
#define mul2(a, b) (1ll * a * b % MOD2)
ll mul(ll a, ll b) {
ull c = (ld)a / M * b + 0.5L, ans = 1ull * a * b - c * M;
return ans < 1ull * M ? ans : ans + M;
}
int power1(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = mul1(a, a))
if (b & 1)
ans = mul1(ans, a);
return ans;
}
int power2(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = mul2(a, a))
if (b & 1)
ans = mul2(ans, a);
return ans;
}
int inv1(int a) { return power1(a, MOD1 - 2); }
int inv2(int b) { return power2(b, MOD2 - 2); }
} // namespace Math
class BigInteger : std::vector<int> {
private:
const static int LEN = 1e6, DIG = 6, MAX = 1e6, LG = 32 - __builtin_clz(LEN), N = 1 << LG;
const static bool NEG = false, POS = true;
static int w1[N + 5], w2[N + 5], rev[N + 5];
bool tag;
class OutOfRange : public std::exception {
const char *what() const throw() { return "Out of range!"; }
};
class DivideByZero : public std::exception {
const char *what() const throw() { return "Divide by 0!"; }
};
void skip() {
int n = (int)size() - 1;
while (n && !at(n))
--n;
resize(n + 1);
}
void add(const BigInteger &a) {
resize(std::max(size(), a.size()));
for (int i = 0; i < (int)a.size(); ++i)
at(i) += a[i];
for (auto itr = begin(); itr != end() - 1; ++itr)
if (*itr >= MAX)
++*(itr + 1), *itr -= MAX;
if (*(end() - 1) >= MAX)
*(end() - 1) -= MAX, push_back(1);
}
void subtract(const BigInteger &a) {
resize(std::max(size(), a.size()));
bool nice = false;
for (int i = 0; i < (int)a.size(); ++i)
at(i) -= a[i], nice = at(i) < 0;
if (size() == a.size() && nice) {
tag = !tag;
for (int &i : *this)
i *= -1;
}
for (auto itr = begin(); itr != end() - 1; ++itr)
if (*itr < 0)
--*(itr + 1), *itr += MAX;
skip();
}
int getLen(int n) {
int lg = 0;
while ((1 << lg) < n)
++lg;
return lg;
}
void init() {
int u = Math::power1(G, (MOD1 - 1) >> LG), v = Math::power2(G, (MOD2 - 1) >> LG);
w1[N >> 1] = w2[N >> 1] = 1;
for (int i = (N >> 1) + 1; i < N; ++i)
w1[i] = mul1(w1[i - 1], u), w2[i] = mul2(w2[i - 1], v);
for (int i = (N >> 1) - 1; i; --i)
w1[i] = w1[i << 1], w2[i] = w2[i << 1];
for (int i = 1; i < N; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (LG - 1));
}
void ntt1(BigInteger &a, int lg, bool inv, int *w) {
if (!rev[1])
init();
int n = 1 << lg;
for (int i = 1; i < n; ++i)
if (i < (rev[i] >> (LG - lg)))
std::swap(a[i], a[rev[i] >> (LG - lg)]);
for (int l = 1; l < n; l <<= 1)
for (int i = 0, *k = w + l; i < n; i += (l << 1))
for (int j = i, *g = k; j < i + l; ++j, ++g) {
int tmp1 = a[j], tmp2 = mul1(*g, a[j + l]);
a[j] = inc1(tmp1, tmp2), a[j + l] = dec1(tmp1, tmp2);
}
if (inv) {
std::reverse(a.data() + 1, a.data() + n);
for (int i = 0, inv = Math::inv1(n); i < n; ++i)
a[i] = mul1(a[i], inv);
}
}
void ntt2(BigInteger &a, int lg, bool inv, int *w) {
if (!rev[1])
init();
int n = 1 << lg;
for (int i = 1; i < n; ++i)
if (i < (rev[i] >> (LG - lg)))
std::swap(a[i], a[rev[i] >> (LG - lg)]);
for (int l = 1; l < n; l <<= 1)
for (int i = 0, *k = w + l; i < n; i += (l << 1))
for (int j = i, *g = k; j < i + l; ++j, ++g) {
int tmp1 = a[j], tmp2 = mul2(*g, a[j + l]);
a[j] = inc2(tmp1, tmp2), a[j + l] = dec2(tmp1, tmp2);
}
if (inv) {
std::reverse(a.data() + 1, a.data() + n);
for (int i = 0, inv = Math::inv2(n); i < n; ++i)
a[i] = mul2(a[i], inv);
}
}
BigInteger &operator<<=(int a) { return insert(begin(), a, 0), *this; }
friend BigInteger operator<<(BigInteger a, int b) { return a <<= b; }
BigInteger &operator>>=(int a) {
if (a >= (int)size())
clear(), push_back(0);
else
erase(begin(), begin() + a);
return *this;
}
friend BigInteger operator>>(BigInteger a, int b) { return a >>= b; }
friend BigInteger inv(const BigInteger &a) {
int n = (int)a.size();
if (n <= 2) {
lll b = 1, c = 0;
for (auto itr = a.rbegin(); itr != a.rend(); ++itr)
b *= MAX, b *= MAX, c *= MAX, c += *itr;
return BigInteger(b / c);
}
int m = (n >> 1) + 1;
BigInteger ans = inv(a >> (n - m)) << (n - m);
return ans * ((BigInteger(2) << n * 2) - ans * a) >> n * 2;
}
public:
BigInteger() { tag = POS, push_back(0); }
template <class T>
BigInteger(T a) {
tag = POS;
if (a == 0) {
push_back(0);
return;
}
if (a < 0)
tag = NEG, a = -a;
while (a)
push_back(a % MAX), a /= MAX;
}
BigInteger(const char *s) {
tag = POS;
if (*s == '-')
tag = NEG, ++s;
int n = 0;
while (isdigit(*s))
++n, ++s;
if (!n) {
tag = POS, push_back(0);
return;
}
resize((n + DIG - 1) / DIG);
for (int i = 0, j = 1; i < n; ++i) {
--s, at(i / DIG) += (*s - '0') * j, j *= 10;
if ((i + 1) % DIG == 0)
j = 1;
}
}
BigInteger(const std::string &s) { BigInteger(s.c_str()); }
friend std::istream &operator>>(std::istream &input, BigInteger &a) {
char c = 0;
while (!isdigit(input.peek()))
c = input.get();
std::string s;
s.clear();
while (isdigit(input.peek()))
s.push_back(input.get());
if (c == '-')
a.tag = NEG;
else
a.tag = POS;
a.clear(), a.resize((s.size() + DIG - 1) / DIG);
auto itr = s.rbegin();
for (int i = 0, j = 1; itr != s.rend(); ++itr, ++i) {
a[i / DIG] += (*itr - '0') * j, j *= 10;
if ((i + 1) % DIG == 0)
j = 1;
}
a.skip();
return input;
}
friend std::ostream &operator<<(std::ostream &output, const BigInteger &a) {
if (a.tag == NEG && !(a.size() == 1 && a[0] == 0))
output << '-';
output << *a.rbegin();
for (auto itr = a.rbegin() + 1; itr != a.rend(); ++itr) {
int tmp = *itr, num[DIG];
for (int i = 0; i < DIG; ++i)
num[i] = tmp % 10, tmp /= 10;
for (int i = DIG; i; --i)
output << num[i - 1];
}
return output;
}
friend bool operator==(const BigInteger &a, const BigInteger &b) {
if (a.size() != b.size())
return false;
for (int i = 0; i < (int)a.size(); ++i)
if (a[i] != b[i])
return false;
return true;
}
friend bool operator!=(const BigInteger &a, const BigInteger &b) { return !(a == b); }
friend bool operator<(const BigInteger &a, const BigInteger &b) {
if (a.tag != b.tag)
return a.tag == NEG;
if (a.size() != b.size())
return (a.size() < b.size()) ^ (a.tag == NEG);
for (int i = (int)a.size() - 1; i >= 0; --i)
if (a[i] > b[i])
return a.tag == NEG;
else if (a[i] < b[i])
return a.tag == POS;
return false;
}
friend bool operator>=(const BigInteger &a, const BigInteger &b) { return !(a < b); }
friend bool operator>(const BigInteger &a, const BigInteger &b) {
if (a.tag != b.tag)
return a.tag == POS;
if (a.size() != b.size())
return (a.size() < b.size()) ^ (a.tag == POS);
for (int i = (int)a.size() - 1; i >= 0; --i)
if (a[i] > b[i])
return a.tag == POS;
else if (a[i] < b[i])
return a.tag == NEG;
return false;
}
friend bool operator<=(const BigInteger &a, const BigInteger &b) { return !(a > b); }
friend BigInteger operator-(BigInteger a) { return a.tag = !a.tag, a; }
BigInteger &operator+=(const BigInteger &a) { return (tag != a.tag) ? subtract(a) : add(a), *this; }
friend BigInteger operator+(BigInteger a, const BigInteger &b) { return a += b; }
BigInteger &operator-=(const BigInteger &a) { return (tag != a.tag) ? add(a) : subtract(a), *this; }
friend BigInteger operator-(BigInteger a, const BigInteger &b) { return a -= b; }
BigInteger &operator*=(BigInteger a) {
tag ^= (a.tag == NEG);
int n = size(), m = a.size(), lg = getLen(n + m);
if (n + m - 1 > LEN)
throw OutOfRange();
resize(1 << lg), a.resize(1 << lg);
BigInteger b = a, c = *this;
ntt1(a, lg, false, w1), ntt1(c, lg, false, w1);
ntt2(*this, lg, false, w2), ntt2(b, lg, false, w2);
for (int i = 0; i < (1 << lg); ++i)
a[i] = mul1(a[i], c[i]), at(i) = mul2(at(i), b[i]);
ntt1(a, lg, true, w1), ntt2(*this, lg, true, w2);
std::vector<ll> tmp(1 << lg);
for (int i = 0; i < (1 << lg); ++i)
tmp[i] = (Math::mul(a[i], M1) + Math::mul(at(i), M2)) % M;
for (int i = 0; i < n + m; ++i)
tmp[i + 1] += tmp[i] / MAX, at(i) = tmp[i] % MAX;
resize(n + m);
if ((int)size() > 1 && *(end() - 1) == 0)
erase(end() - 1);
return *this;
}
friend BigInteger operator*(BigInteger a, const BigInteger &b) { return a *= b; }
BigInteger &operator/=(BigInteger a) {
if (a == 0)
throw DivideByZero();
bool tmp = (tag ^ a.tag) ? NEG : POS;
tag = a.tag = POS;
int n = size(), m = a.size();
if (n > m * 2)
*this <<= (n - m * 2), a <<= (n - m * 2), m = n - m, n = m * 2;
BigInteger b = *this * inv(a) >> m * 2;
*this -= a * b, *this = *this < a ? b : b + 1;
tag = tmp;
return *this;
}
friend BigInteger operator/(BigInteger a, const BigInteger &b) { return a /= b; }
BigInteger &operator%=(BigInteger a) {
if (a == 0)
throw DivideByZero();
bool tmp = tag;
tag = a.tag = POS;
int n = size(), m = a.size();
if (n > m * 2)
*this <<= (n - m * 2), a <<= (n - m * 2), m = n - m, n = m * 2;
BigInteger b = *this * inv(a) >> m * 2;
*this -= a * b;
if (*this >= a)
*this -= a;
tag = tmp;
return *this;
}
friend BigInteger operator%(BigInteger a, const BigInteger &b) { return a %= b; }
friend BigInteger operator^(BigInteger a, int b) {
BigInteger ans = 1;
for (; b; b >>= 1, a *= a)
if (b & 1)
ans *= a;
return ans;
}
};
int BigInteger::w1[], BigInteger::w2[], BigInteger::rev[];
#undef G
#undef MOD1
#undef MOD2
#undef M
#undef M1
#undef M2
#undef lg
#undef inc1
#undef inc2
#undef dec1
#undef dec2
#undef mul1
#undef mul2
} // namespace Integer
|