AC Library

[AC Library](atcoder/ac-library: AtCoder Library) 是 AtCoder 官方推出的已经完全封装的缺省源代码,为了减少参赛者在像线段树、平衡树等数据结构或者取模方面浪费过多的时间,允行直接进行调用。

首先先前往 Releases · atcoder/ac-library 下载最新版的文件。解压后里面的 atcoder 文件夹即为整个库,复制后直接丢进本地编译器的库文件中,差不多类似 mingw64/lib/gcc/.../include/c++ 的文件夹中。

使用方法

头文件引用:#include <atcoder/all>

名称标签声明:using namespace atcoder

modint

位于 <atcoder/modint> 当中,使用时需要先选择一个模数。

如果模数 mod\bmod99824435399824435310000000071000000007​,那么有现成的类可用,使用定义:typedef atcder::modint998244353 mint 即可。

10000000091000000009 同样也有,使用 typedef static_modint<1000000009> mint 定义。

如果模数固定,可以使用 modint::set_mod(int m) 进行更改。

如果模数不固定或者同时多种模数,使用:

1
2
3
4
using mint0 = dynamic_modint<114514>;
using mint1 = dynamic_modint<1919810>;

mint0 a = 123; mint1 b = 234;

封装了以下几种函数:

  • pow(int k),返回 这个数模上模数的 kk 次方,复杂度 O(logk)O(\log k)
  • inv(),返回这个数在模数下的逆元,复杂度 O(logmod)O(\log \text{mod})

math

实力太弱感觉没有什么能用的……

  • pow_mod(LL x, LL n, LL m),返回 xnmodmx ^ n \bmod m

  • inv_mod(LL x, LL m),返回 y (0y<m,xy(modm))y\ (0 \le y \lt m, xy \pmod m)

  • pair<LL, LL> crt(vector<LL> r, vector<LL> m),中国剩余定理,能用到的话很爽。

dsu

并查集,啥都有。

  • dsu D(int N),建一个 0n10 \sim n - 1 的并查集。

  • D.merge(int a, int b),合并 aabb 所属的并查集。

  • D.same(int a, int b),判断是否在一个并查集中。

  • D.leader(int a),返回 aa 所属并查集的祖先。

  • D.size(int a),返回 aa 所属并查集的大小。

Other

有 线段树,2-SAT,强连通分量,网咯最大流,树状数组,字符串(SA,LCP,Z 函数),卷积。很少用,或者扩展性不强,就没写上来。

缺省源

由于只能在 AtCoder 使用,别的刷题网站没有库,非常的不好,于是干脆直接把库文件复制出来即可(最好不要展开)。

modint

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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#ifndef ATCODER_INTERNAL_MATH_HPP
#define ATCODER_INTERNAL_MATH_HPP 1

#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
unsigned int _m;
unsigned long long im;

// @param m `1 <= m`
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

// @return m
unsigned int umod() const { return _m; }

// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay

// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};

// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;

while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b

// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b

auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}

unsigned long long y_max = a * n + b;
if (y_max < m) break;
// y_max < m * (n + 1)
// floor(y_max / m) <= n
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}

} // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_MATH_HPP

#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP
#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

} // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP

#ifndef ATCODER_MODINT_HPP
#define ATCODER_MODINT_HPP 1

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

} // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;

public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}

static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}

unsigned int val() const { return _v; }

mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}

mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }

mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}

friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}

private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;

public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}

dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}

unsigned int val() const { return _v; }

mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}

mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }

mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}

friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}

private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

} // namespace internal

} // namespace atcoder

#endif // ATCODER_MODINT_HPP

dsu

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
#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
public:
dsu() : _n(0) {}
explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}

int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y) return x;
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}

bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}

int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}

int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}

std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(
std::remove_if(result.begin(), result.end(),
[&](const std::vector<int>& v) { return v.empty(); }),
result.end());
return result;
}

private:
int _n;
// root node: -1 * component size
// otherwise: parent
std::vector<int> parent_or_size;
};

} // namespace atcoder

#endif // ATCODER_DSU_HPP

math

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
#ifndef ATCODER_INTERNAL_MATH_HPP
#define ATCODER_INTERNAL_MATH_HPP 1

#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
unsigned int _m;
unsigned long long im;

// @param m `1 <= m`
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

// @return m
unsigned int umod() const { return _m; }

// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay

// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};

// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;

while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b

// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b

auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}

unsigned long long y_max = a * n + b;
if (y_max < m) break;
// y_max < m * (n + 1)
// floor(y_max / m) <= n
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}

} // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_MATH_HPP

#ifndef ATCODER_MATH_HPP
#define ATCODER_MATH_HPP 1

#include <algorithm>
#include <cassert>
#include <tuple>
#include <vector>

// #include "atcoder/internal_math"

namespace atcoder {

long long pow_mod(long long x, long long n, int m) {
assert(0 <= n && 1 <= m);
if (m == 1) return 0;
internal::barrett bt((unsigned int)(m));
unsigned int r = 1, y = (unsigned int)(internal::safe_mod(x, m));
while (n) {
if (n & 1) r = bt.mul(r, y);
y = bt.mul(y, y);
n >>= 1;
}
return r;
}

long long inv_mod(long long x, long long m) {
assert(1 <= m);
auto z = internal::inv_gcd(x, m);
assert(z.first == 1);
return z.second;
}

// (rem, mod)
std::pair<long long, long long> crt(const std::vector<long long>& r,
const std::vector<long long>& m) {
assert(r.size() == m.size());
int n = int(r.size());
// Contracts: 0 <= r0 < m0
long long r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
assert(1 <= m[i]);
long long r1 = internal::safe_mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) {
std::swap(r0, r1);
std::swap(m0, m1);
}
if (m0 % m1 == 0) {
if (r0 % m1 != r1) return {0, 0};
continue;
}
// assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)

// (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));
// r2 % m0 = r0
// r2 % m1 = r1
// -> (r0 + x*m0) % m1 = r1
// -> x*u0*g = r1-r0 (mod u1*g) (u0*g = m0, u1*g = m1)
// -> x = (r1 - r0) / g * inv(u0) (mod u1)

// im = inv(u0) (mod u1) (0 <= im < u1)
long long g, im;
std::tie(g, im) = internal::inv_gcd(m0, m1);

long long u1 = (m1 / g);
// |r1 - r0| < (m0 + m1) <= lcm(m0, m1)
if ((r1 - r0) % g) return {0, 0};

// u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)
long long x = (r1 - r0) / g % u1 * im % u1;

// |r0| + |m0 * x|
// < m0 + m0 * (u1 - 1)
// = m0 + m0 * m1 / g - m0
// = lcm(m0, m1)
r0 += x * m0;
m0 *= u1; // -> lcm(m0, m1)
if (r0 < 0) r0 += m0;
}
return {r0, m0};
}

long long floor_sum(long long n, long long m, long long a, long long b) {
assert(0 <= n && n < (1LL << 32));
assert(1 <= m && m < (1LL << 32));
unsigned long long ans = 0;
if (a < 0) {
unsigned long long a2 = internal::safe_mod(a, m);
ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
a = a2;
}
if (b < 0) {
unsigned long long b2 = internal::safe_mod(b, m);
ans -= 1ULL * n * ((b2 - b) / m);
b = b2;
}
return ans + internal::floor_sum_unsigned(n, m, a, b);
}

} // namespace atcoder

#endif // ATCODER_MATH_HPP

以上只用于临时使用,如果想要把缺省源放进 VS Code User Snippets 呢?

User Snippets

acmath

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
"Atcoder Math Template": {
"prefix": "acmath",
"body": [
"#ifndef ATCODER_INTERNAL_MATH_HPP",
"#define ATCODER_INTERNAL_MATH_HPP 1",
"",
"#include <utility>",
"",
"#ifdef _MSC_VER",
"#include <intrin.h>",
"#endif",
"",
"namespace atcoder {",
"",
"namespace internal {",
"",
"// @param m `1 <= m`",
"// @return x mod m",
"constexpr long long safe_mod(long long x, long long m) {",
" x %= m;",
" if (x < 0) x += m;",
" return x;",
"}",
"",
"// Fast modular multiplication by barrett reduction",
"// Reference: https://en.wikipedia.org/wiki/Barrett_reduction",
"// NOTE: reconsider after Ice Lake",
"struct barrett {",
" unsigned int _m;",
" unsigned long long im;",
"",
" // @param m `1 <= m`",
" explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}",
"",
" // @return m",
" unsigned int umod() const { return _m; }",
"",
" // @param a `0 <= a < m`",
" // @param b `0 <= b < m`",
" // @return `a * b % m`",
" unsigned int mul(unsigned int a, unsigned int b) const {",
" // [1] m = 1",
" // a = b = im = 0, so okay",
"",
" // [2] m >= 2",
" // im = ceil(2^64 / m)",
" // -> im * m = 2^64 + r (0 <= r < m)",
" // let z = a*b = c*m + d (0 <= c, d < m)",
" // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im",
" // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2",
" // ((ab * im) >> 64) == c or c + 1",
" unsigned long long z = a;",
" z *= b;",
"#ifdef _MSC_VER",
" unsigned long long x;",
" _umul128(z, im, &x);",
"#else",
" unsigned long long x =",
" (unsigned long long)(((unsigned __int128)(z)*im) >> 64);",
"#endif",
" unsigned long long y = x * _m;",
" return (unsigned int)(z - y + (z < y ? _m : 0));",
" }",
"};",
"",
"// @param n `0 <= n`",
"// @param m `1 <= m`",
"// @return `(x ** n) % m`",
"constexpr long long pow_mod_constexpr(long long x, long long n, int m) {",
" if (m == 1) return 0;",
" unsigned int _m = (unsigned int)(m);",
" unsigned long long r = 1;",
" unsigned long long y = safe_mod(x, m);",
" while (n) {",
" if (n & 1) r = (r * y) % _m;",
" y = (y * y) % _m;",
" n >>= 1;",
" }",
" return r;",
"}",
"",
"// Reference:",
"// M. Forisek and J. Jancina,",
"// Fast Primality Testing for Integers That Fit into a Machine Word",
"// @param n `0 <= n`",
"constexpr bool is_prime_constexpr(int n) {",
" if (n <= 1) return false;",
" if (n == 2 || n == 7 || n == 61) return true;",
" if (n % 2 == 0) return false;",
" long long d = n - 1;",
" while (d % 2 == 0) d /= 2;",
" constexpr long long bases[3] = {2, 7, 61};",
" for (long long a : bases) {",
" long long t = d;",
" long long y = pow_mod_constexpr(a, t, n);",
" while (t != n - 1 && y != 1 && y != n - 1) {",
" y = y * y % n;",
" t <<= 1;",
" }",
" if (y != n - 1 && t % 2 == 0) {",
" return false;",
" }",
" }",
" return true;",
"}",
"template <int n> constexpr bool is_prime = is_prime_constexpr(n);",
"",
"// @param b `1 <= b`",
"// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g",
"constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {",
" a = safe_mod(a, b);",
" if (a == 0) return {b, 0};",
"",
" // Contracts:",
" // [1] s - m0 * a = 0 (mod b)",
" // [2] t - m1 * a = 0 (mod b)",
" // [3] s * |m1| + t * |m0| <= b",
" long long s = b, t = a;",
" long long m0 = 0, m1 = 1;",
"",
" while (t) {",
" long long u = s / t;",
" s -= t * u;",
" m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b",
"",
" // [3]:",
" // (s - t * u) * |m1| + t * |m0 - m1 * u|",
" // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)",
" // = s * |m1| + t * |m0| <= b",
"",
" auto tmp = s;",
" s = t;",
" t = tmp;",
" tmp = m0;",
" m0 = m1;",
" m1 = tmp;",
" }",
" // by [3]: |m0| <= b/g",
" // by g != b: |m0| < b/g",
" if (m0 < 0) m0 += b / s;",
" return {s, m0};",
"}",
"",
"// Compile time primitive root",
"// @param m must be prime",
"// @return primitive root (and minimum in now)",
"constexpr int primitive_root_constexpr(int m) {",
" if (m == 2) return 1;",
" if (m == 167772161) return 3;",
" if (m == 469762049) return 3;",
" if (m == 754974721) return 11;",
" if (m == 998244353) return 3;",
" int divs[20] = {};",
" divs[0] = 2;",
" int cnt = 1;",
" int x = (m - 1) / 2;",
" while (x % 2 == 0) x /= 2;",
" for (int i = 3; (long long)(i)*i <= x; i += 2) {",
" if (x % i == 0) {",
" divs[cnt++] = i;",
" while (x % i == 0) {",
" x /= i;",
" }",
" }",
" }",
" if (x > 1) {",
" divs[cnt++] = x;",
" }",
" for (int g = 2;; g++) {",
" bool ok = true;",
" for (int i = 0; i < cnt; i++) {",
" if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {",
" ok = false;",
" break;",
" }",
" }",
" if (ok) return g;",
" }",
"}",
"template <int m> constexpr int primitive_root = primitive_root_constexpr(m);",
"",
"// @param n `n < 2^32`",
"// @param m `1 <= m < 2^32`",
"// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)",
"unsigned long long floor_sum_unsigned(unsigned long long n,",
" unsigned long long m,",
" unsigned long long a,",
" unsigned long long b) {",
" unsigned long long ans = 0;",
" while (true) {",
" if (a >= m) {",
" ans += n * (n - 1) / 2 * (a / m);",
" a %= m;",
" }",
" if (b >= m) {",
" ans += n * (b / m);",
" b %= m;",
" }",
"",
" unsigned long long y_max = a * n + b;",
" if (y_max < m) break;",
" // y_max < m * (n + 1)",
" // floor(y_max / m) <= n",
" n = (unsigned long long)(y_max / m);",
" b = (unsigned long long)(y_max % m);",
" std::swap(m, a);",
" }",
" return ans;",
"}",
"",
"} // namespace internal",
"",
"} // namespace atcoder",
"",
"#endif // ATCODER_INTERNAL_MATH_HPP",
"",
"#ifndef ATCODER_MATH_HPP",
"#define ATCODER_MATH_HPP 1",
"",
"#include <algorithm>",
"#include <cassert>",
"#include <tuple>",
"#include <vector>",
"",
"// #include \"atcoder/internal_math\"",
"",
"namespace atcoder {",
"",
"long long pow_mod(long long x, long long n, int m) {",
" assert(0 <= n && 1 <= m);",
" if (m == 1) return 0;",
" internal::barrett bt((unsigned int)(m));",
" unsigned int r = 1, y = (unsigned int)(internal::safe_mod(x, m));",
" while (n) {",
" if (n & 1) r = bt.mul(r, y);",
" y = bt.mul(y, y);",
" n >>= 1;",
" }",
" return r;",
"}",
"",
"long long inv_mod(long long x, long long m) {",
" assert(1 <= m);",
" auto z = internal::inv_gcd(x, m);",
" assert(z.first == 1);",
" return z.second;",
"}",
"",
"// (rem, mod)",
"std::pair<long long, long long> crt(const std::vector<long long>& r,",
" const std::vector<long long>& m) {",
" assert(r.size() == m.size());",
" int n = int(r.size());",
" // Contracts: 0 <= r0 < m0",
" long long r0 = 0, m0 = 1;",
" for (int i = 0; i < n; i++) {",
" assert(1 <= m[i]);",
" long long r1 = internal::safe_mod(r[i], m[i]), m1 = m[i];",
" if (m0 < m1) {",
" std::swap(r0, r1);",
" std::swap(m0, m1);",
" }",
" if (m0 % m1 == 0) {",
" if (r0 % m1 != r1) return {0, 0};",
" continue;",
" }",
" // assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)",
"",
" // (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));",
" // r2 % m0 = r0",
" // r2 % m1 = r1",
" // -> (r0 + x*m0) % m1 = r1",
" // -> x*u0*g = r1-r0 (mod u1*g) (u0*g = m0, u1*g = m1)",
" // -> x = (r1 - r0) / g * inv(u0) (mod u1)",
"",
" // im = inv(u0) (mod u1) (0 <= im < u1)",
" long long g, im;",
" std::tie(g, im) = internal::inv_gcd(m0, m1);",
"",
" long long u1 = (m1 / g);",
" // |r1 - r0| < (m0 + m1) <= lcm(m0, m1)",
" if ((r1 - r0) % g) return {0, 0};",
"",
" // u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)",
" long long x = (r1 - r0) / g % u1 * im % u1;",
"",
" // |r0| + |m0 * x|",
" // < m0 + m0 * (u1 - 1)",
" // = m0 + m0 * m1 / g - m0",
" // = lcm(m0, m1)",
" r0 += x * m0;",
" m0 *= u1; // -> lcm(m0, m1)",
" if (r0 < 0) r0 += m0;",
" }",
" return {r0, m0};",
"}",
"",
"long long floor_sum(long long n, long long m, long long a, long long b) {",
" assert(0 <= n && n < (1LL << 32));",
" assert(1 <= m && m < (1LL << 32));",
" unsigned long long ans = 0;",
" if (a < 0) {",
" unsigned long long a2 = internal::safe_mod(a, m);",
" ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);",
" a = a2;",
" }",
" if (b < 0) {",
" unsigned long long b2 = internal::safe_mod(b, m);",
" ans -= 1ULL * n * ((b2 - b) / m);",
" b = b2;",
" }",
" return ans + internal::floor_sum_unsigned(n, m, a, b);",
"}",
"",
"} // namespace atcoder",
"",
"#endif // ATCODER_MATH_HPP"
],
"description": "A Convenient Template For Math"
}

acmodint

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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
"Atcoder Modint Template": {
"prefix": "acmodint",
"body": [
"#ifndef ATCODER_INTERNAL_MATH_HPP",
"#define ATCODER_INTERNAL_MATH_HPP 1",
"",
"#include <utility>",
"",
"#ifdef _MSC_VER",
"#include <intrin.h>",
"#endif",
"",
"namespace atcoder {",
"",
"namespace internal {",
"",
"// @param m `1 <= m`",
"// @return x mod m",
"constexpr long long safe_mod(long long x, long long m) {",
" x %= m;",
" if (x < 0) x += m;",
" return x;",
"}",
"",
"// Fast modular multiplication by barrett reduction",
"// Reference: https://en.wikipedia.org/wiki/Barrett_reduction",
"// NOTE: reconsider after Ice Lake",
"struct barrett {",
" unsigned int _m;",
" unsigned long long im;",
"",
" // @param m `1 <= m`",
" explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}",
"",
" // @return m",
" unsigned int umod() const { return _m; }",
"",
" // @param a `0 <= a < m`",
" // @param b `0 <= b < m`",
" // @return `a * b % m`",
" unsigned int mul(unsigned int a, unsigned int b) const {",
" // [1] m = 1",
" // a = b = im = 0, so okay",
"",
" // [2] m >= 2",
" // im = ceil(2^64 / m)",
" // -> im * m = 2^64 + r (0 <= r < m)",
" // let z = a*b = c*m + d (0 <= c, d < m)",
" // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im",
" // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2",
" // ((ab * im) >> 64) == c or c + 1",
" unsigned long long z = a;",
" z *= b;",
"#ifdef _MSC_VER",
" unsigned long long x;",
" _umul128(z, im, &x);",
"#else",
" unsigned long long x =",
" (unsigned long long)(((unsigned __int128)(z)*im) >> 64);",
"#endif",
" unsigned long long y = x * _m;",
" return (unsigned int)(z - y + (z < y ? _m : 0));",
" }",
"};",
"",
"// @param n `0 <= n`",
"// @param m `1 <= m`",
"// @return `(x ** n) % m`",
"constexpr long long pow_mod_constexpr(long long x, long long n, int m) {",
" if (m == 1) return 0;",
" unsigned int _m = (unsigned int)(m);",
" unsigned long long r = 1;",
" unsigned long long y = safe_mod(x, m);",
" while (n) {",
" if (n & 1) r = (r * y) % _m;",
" y = (y * y) % _m;",
" n >>= 1;",
" }",
" return r;",
"}",
"",
"// Reference:",
"// M. Forisek and J. Jancina,",
"// Fast Primality Testing for Integers That Fit into a Machine Word",
"// @param n `0 <= n`",
"constexpr bool is_prime_constexpr(int n) {",
" if (n <= 1) return false;",
" if (n == 2 || n == 7 || n == 61) return true;",
" if (n % 2 == 0) return false;",
" long long d = n - 1;",
" while (d % 2 == 0) d /= 2;",
" constexpr long long bases[3] = {2, 7, 61};",
" for (long long a : bases) {",
" long long t = d;",
" long long y = pow_mod_constexpr(a, t, n);",
" while (t != n - 1 && y != 1 && y != n - 1) {",
" y = y * y % n;",
" t <<= 1;",
" }",
" if (y != n - 1 && t % 2 == 0) {",
" return false;",
" }",
" }",
" return true;",
"}",
"template <int n> constexpr bool is_prime = is_prime_constexpr(n);",
"",
"// @param b `1 <= b`",
"// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g",
"constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {",
" a = safe_mod(a, b);",
" if (a == 0) return {b, 0};",
"",
" // Contracts:",
" // [1] s - m0 * a = 0 (mod b)",
" // [2] t - m1 * a = 0 (mod b)",
" // [3] s * |m1| + t * |m0| <= b",
" long long s = b, t = a;",
" long long m0 = 0, m1 = 1;",
"",
" while (t) {",
" long long u = s / t;",
" s -= t * u;",
" m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b",
"",
" // [3]:",
" // (s - t * u) * |m1| + t * |m0 - m1 * u|",
" // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)",
" // = s * |m1| + t * |m0| <= b",
"",
" auto tmp = s;",
" s = t;",
" t = tmp;",
" tmp = m0;",
" m0 = m1;",
" m1 = tmp;",
" }",
" // by [3]: |m0| <= b/g",
" // by g != b: |m0| < b/g",
" if (m0 < 0) m0 += b / s;",
" return {s, m0};",
"}",
"",
"// Compile time primitive root",
"// @param m must be prime",
"// @return primitive root (and minimum in now)",
"constexpr int primitive_root_constexpr(int m) {",
" if (m == 2) return 1;",
" if (m == 167772161) return 3;",
" if (m == 469762049) return 3;",
" if (m == 754974721) return 11;",
" if (m == 998244353) return 3;",
" int divs[20] = {};",
" divs[0] = 2;",
" int cnt = 1;",
" int x = (m - 1) / 2;",
" while (x % 2 == 0) x /= 2;",
" for (int i = 3; (long long)(i)*i <= x; i += 2) {",
" if (x % i == 0) {",
" divs[cnt++] = i;",
" while (x % i == 0) {",
" x /= i;",
" }",
" }",
" }",
" if (x > 1) {",
" divs[cnt++] = x;",
" }",
" for (int g = 2;; g++) {",
" bool ok = true;",
" for (int i = 0; i < cnt; i++) {",
" if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {",
" ok = false;",
" break;",
" }",
" }",
" if (ok) return g;",
" }",
"}",
"template <int m> constexpr int primitive_root = primitive_root_constexpr(m);",
"",
"// @param n `n < 2^32`",
"// @param m `1 <= m < 2^32`",
"// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)",
"unsigned long long floor_sum_unsigned(unsigned long long n,",
" unsigned long long m,",
" unsigned long long a,",
" unsigned long long b) {",
" unsigned long long ans = 0;",
" while (true) {",
" if (a >= m) {",
" ans += n * (n - 1) / 2 * (a / m);",
" a %= m;",
" }",
" if (b >= m) {",
" ans += n * (b / m);",
" b %= m;",
" }",
"",
" unsigned long long y_max = a * n + b;",
" if (y_max < m) break;",
" // y_max < m * (n + 1)",
" // floor(y_max / m) <= n",
" n = (unsigned long long)(y_max / m);",
" b = (unsigned long long)(y_max % m);",
" std::swap(m, a);",
" }",
" return ans;",
"}",
"",
"} // namespace internal",
"",
"} // namespace atcoder",
"",
"#endif // ATCODER_INTERNAL_MATH_HPP",
"",
"#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP",
"#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1",
"",
"#include <cassert>",
"#include <numeric>",
"#include <type_traits>",
"",
"namespace atcoder {",
"",
"namespace internal {",
"",
"#ifndef _MSC_VER",
"template <class T>",
"using is_signed_int128 =",
" typename std::conditional<std::is_same<T, __int128_t>::value ||",
" std::is_same<T, __int128>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using is_unsigned_int128 =",
" typename std::conditional<std::is_same<T, __uint128_t>::value ||",
" std::is_same<T, unsigned __int128>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using make_unsigned_int128 =",
" typename std::conditional<std::is_same<T, __int128_t>::value,",
" __uint128_t,",
" unsigned __int128>;",
"",
"template <class T>",
"using is_integral = typename std::conditional<std::is_integral<T>::value ||",
" is_signed_int128<T>::value ||",
" is_unsigned_int128<T>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using is_signed_int = typename std::conditional<(is_integral<T>::value &&",
" std::is_signed<T>::value) ||",
" is_signed_int128<T>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using is_unsigned_int =",
" typename std::conditional<(is_integral<T>::value &&",
" std::is_unsigned<T>::value) ||",
" is_unsigned_int128<T>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using to_unsigned = typename std::conditional<",
" is_signed_int128<T>::value,",
" make_unsigned_int128<T>,",
" typename std::conditional<std::is_signed<T>::value,",
" std::make_unsigned<T>,",
" std::common_type<T>>::type>::type;",
"",
"#else",
"",
"template <class T> using is_integral = typename std::is_integral<T>;",
"",
"template <class T>",
"using is_signed_int =",
" typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using is_unsigned_int =",
" typename std::conditional<is_integral<T>::value &&",
" std::is_unsigned<T>::value,",
" std::true_type,",
" std::false_type>::type;",
"",
"template <class T>",
"using to_unsigned = typename std::conditional<is_signed_int<T>::value,",
" std::make_unsigned<T>,",
" std::common_type<T>>::type;",
"",
"#endif",
"",
"template <class T>",
"using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;",
"",
"template <class T>",
"using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;",
"",
"template <class T> using to_unsigned_t = typename to_unsigned<T>::type;",
"",
"} // namespace internal",
"",
"} // namespace atcoder",
"",
"#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP",
"",
"#ifndef ATCODER_MODINT_HPP",
"#define ATCODER_MODINT_HPP 1",
"",
"#include <cassert>",
"#include <numeric>",
"#include <type_traits>",
"",
"#ifdef _MSC_VER",
"#include <intrin.h>",
"#endif",
"",
"namespace atcoder {",
"",
"namespace internal {",
"",
"struct modint_base {};",
"struct static_modint_base : modint_base {};",
"",
"template <class T> using is_modint = std::is_base_of<modint_base, T>;",
"template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;",
"",
"} // namespace internal",
"",
"template <int m, std::enable_if_t<(1 <= m)>* = nullptr>",
"struct static_modint : internal::static_modint_base {",
" using mint = static_modint;",
"",
" public:",
" static constexpr int mod() { return m; }",
" static mint raw(int v) {",
" mint x;",
" x._v = v;",
" return x;",
" }",
"",
" static_modint() : _v(0) {}",
" template <class T, internal::is_signed_int_t<T>* = nullptr>",
" static_modint(T v) {",
" long long x = (long long)(v % (long long)(umod()));",
" if (x < 0) x += umod();",
" _v = (unsigned int)(x);",
" }",
" template <class T, internal::is_unsigned_int_t<T>* = nullptr>",
" static_modint(T v) {",
" _v = (unsigned int)(v % umod());",
" }",
"",
" unsigned int val() const { return _v; }",
"",
" mint& operator++() {",
" _v++;",
" if (_v == umod()) _v = 0;",
" return *this;",
" }",
" mint& operator--() {",
" if (_v == 0) _v = umod();",
" _v--;",
" return *this;",
" }",
" mint operator++(int) {",
" mint result = *this;",
" ++*this;",
" return result;",
" }",
" mint operator--(int) {",
" mint result = *this;",
" --*this;",
" return result;",
" }",
"",
" mint& operator+=(const mint& rhs) {",
" _v += rhs._v;",
" if (_v >= umod()) _v -= umod();",
" return *this;",
" }",
" mint& operator-=(const mint& rhs) {",
" _v -= rhs._v;",
" if (_v >= umod()) _v += umod();",
" return *this;",
" }",
" mint& operator*=(const mint& rhs) {",
" unsigned long long z = _v;",
" z *= rhs._v;",
" _v = (unsigned int)(z % umod());",
" return *this;",
" }",
" mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }",
"",
" mint operator+() const { return *this; }",
" mint operator-() const { return mint() - *this; }",
"",
" mint pow(long long n) const {",
" assert(0 <= n);",
" mint x = *this, r = 1;",
" while (n) {",
" if (n & 1) r *= x;",
" x *= x;",
" n >>= 1;",
" }",
" return r;",
" }",
" mint inv() const {",
" if (prime) {",
" assert(_v);",
" return pow(umod() - 2);",
" } else {",
" auto eg = internal::inv_gcd(_v, m);",
" assert(eg.first == 1);",
" return eg.second;",
" }",
" }",
"",
" friend mint operator+(const mint& lhs, const mint& rhs) {",
" return mint(lhs) += rhs;",
" }",
" friend mint operator-(const mint& lhs, const mint& rhs) {",
" return mint(lhs) -= rhs;",
" }",
" friend mint operator*(const mint& lhs, const mint& rhs) {",
" return mint(lhs) *= rhs;",
" }",
" friend mint operator/(const mint& lhs, const mint& rhs) {",
" return mint(lhs) /= rhs;",
" }",
" friend bool operator==(const mint& lhs, const mint& rhs) {",
" return lhs._v == rhs._v;",
" }",
" friend bool operator!=(const mint& lhs, const mint& rhs) {",
" return lhs._v != rhs._v;",
" }",
"",
" private:",
" unsigned int _v;",
" static constexpr unsigned int umod() { return m; }",
" static constexpr bool prime = internal::is_prime<m>;",
"};",
"",
"template <int id> struct dynamic_modint : internal::modint_base {",
" using mint = dynamic_modint;",
"",
" public:",
" static int mod() { return (int)(bt.umod()); }",
" static void set_mod(int m) {",
" assert(1 <= m);",
" bt = internal::barrett(m);",
" }",
" static mint raw(int v) {",
" mint x;",
" x._v = v;",
" return x;",
" }",
"",
" dynamic_modint() : _v(0) {}",
" template <class T, internal::is_signed_int_t<T>* = nullptr>",
" dynamic_modint(T v) {",
" long long x = (long long)(v % (long long)(mod()));",
" if (x < 0) x += mod();",
" _v = (unsigned int)(x);",
" }",
" template <class T, internal::is_unsigned_int_t<T>* = nullptr>",
" dynamic_modint(T v) {",
" _v = (unsigned int)(v % mod());",
" }",
"",
" unsigned int val() const { return _v; }",
"",
" mint& operator++() {",
" _v++;",
" if (_v == umod()) _v = 0;",
" return *this;",
" }",
" mint& operator--() {",
" if (_v == 0) _v = umod();",
" _v--;",
" return *this;",
" }",
" mint operator++(int) {",
" mint result = *this;",
" ++*this;",
" return result;",
" }",
" mint operator--(int) {",
" mint result = *this;",
" --*this;",
" return result;",
" }",
"",
" mint& operator+=(const mint& rhs) {",
" _v += rhs._v;",
" if (_v >= umod()) _v -= umod();",
" return *this;",
" }",
" mint& operator-=(const mint& rhs) {",
" _v += mod() - rhs._v;",
" if (_v >= umod()) _v -= umod();",
" return *this;",
" }",
" mint& operator*=(const mint& rhs) {",
" _v = bt.mul(_v, rhs._v);",
" return *this;",
" }",
" mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }",
"",
" mint operator+() const { return *this; }",
" mint operator-() const { return mint() - *this; }",
"",
" mint pow(long long n) const {",
" assert(0 <= n);",
" mint x = *this, r = 1;",
" while (n) {",
" if (n & 1) r *= x;",
" x *= x;",
" n >>= 1;",
" }",
" return r;",
" }",
" mint inv() const {",
" auto eg = internal::inv_gcd(_v, mod());",
" assert(eg.first == 1);",
" return eg.second;",
" }",
"",
" friend mint operator+(const mint& lhs, const mint& rhs) {",
" return mint(lhs) += rhs;",
" }",
" friend mint operator-(const mint& lhs, const mint& rhs) {",
" return mint(lhs) -= rhs;",
" }",
" friend mint operator*(const mint& lhs, const mint& rhs) {",
" return mint(lhs) *= rhs;",
" }",
" friend mint operator/(const mint& lhs, const mint& rhs) {",
" return mint(lhs) /= rhs;",
" }",
" friend bool operator==(const mint& lhs, const mint& rhs) {",
" return lhs._v == rhs._v;",
" }",
" friend bool operator!=(const mint& lhs, const mint& rhs) {",
" return lhs._v != rhs._v;",
" }",
"",
" private:",
" unsigned int _v;",
" static internal::barrett bt;",
" static unsigned int umod() { return bt.umod(); }",
"};",
"template <int id> internal::barrett dynamic_modint<id>::bt(998244353);",
"",
"using modint998244353 = static_modint<998244353>;",
"using modint1000000007 = static_modint<1000000007>;",
"using modint = dynamic_modint<-1>;",
"",
"namespace internal {",
"",
"template <class T>",
"using is_static_modint = std::is_base_of<internal::static_modint_base, T>;",
"",
"template <class T>",
"using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;",
"",
"template <class> struct is_dynamic_modint : public std::false_type {};",
"template <int id>",
"struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};",
"",
"template <class T>",
"using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;",
"",
"} // namespace internal",
"",
"} // namespace atcoder",
"",
"#endif // ATCODER_MODINT_HPP",
""
],
"description": "A Convenient Template For Mods"
}

acdsu

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
"Atcoder DSU Template": {
"prefix": "acdsu",
"body": [
"#ifndef ATCODER_DSU_HPP",
"#define ATCODER_DSU_HPP 1",
"",
"#include <algorithm>",
"#include <cassert>",
"#include <vector>",
"",
"namespace atcoder {",
"",
"// Implement (union by size) + (path compression)",
"// Reference:",
"// Zvi Galil and Giuseppe F. Italiano,",
"// Data structures and algorithms for disjoint set union problems",
"struct dsu {",
" public:",
" dsu() : _n(0) {}",
" explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}",
"",
" int merge(int a, int b) {",
" assert(0 <= a && a < _n);",
" assert(0 <= b && b < _n);",
" int x = leader(a), y = leader(b);",
" if (x == y) return x;",
" if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);",
" parent_or_size[x] += parent_or_size[y];",
" parent_or_size[y] = x;",
" return x;",
" }",
"",
" bool same(int a, int b) {",
" assert(0 <= a && a < _n);",
" assert(0 <= b && b < _n);",
" return leader(a) == leader(b);",
" }",
"",
" int leader(int a) {",
" assert(0 <= a && a < _n);",
" if (parent_or_size[a] < 0) return a;",
" return parent_or_size[a] = leader(parent_or_size[a]);",
" }",
"",
" int size(int a) {",
" assert(0 <= a && a < _n);",
" return -parent_or_size[leader(a)];",
" }",
"",
" std::vector<std::vector<int>> groups() {",
" std::vector<int> leader_buf(_n), group_size(_n);",
" for (int i = 0; i < _n; i++) {",
" leader_buf[i] = leader(i);",
" group_size[leader_buf[i]]++;",
" }",
" std::vector<std::vector<int>> result(_n);",
" for (int i = 0; i < _n; i++) {",
" result[i].reserve(group_size[i]);",
" }",
" for (int i = 0; i < _n; i++) {",
" result[leader_buf[i]].push_back(i);",
" }",
" result.erase(",
" std::remove_if(result.begin(), result.end(),",
" [&](const std::vector<int>& v) { return v.empty(); }),",
" result.end());",
" return result;",
" }",
"",
" private:",
" int _n;",
" // root node: -1 * component size",
" // otherwise: parent",
" std::vector<int> parent_or_size;",
"};",
"",
"} // namespace atcoder",
"",
"#endif // ATCODER_DSU_HPP",
""
],
"description": "A Convenient Template For Dsu"
}