-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.code-snippets
9417 lines (9417 loc) · 379 KB
/
algo.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"lis.cpp": {
"prefix": "lis",
"scope": "cpp",
"body": [
"template<typename T = int, typename cmp = less<T>>",
"int lis(const vector<T> &v){",
" vector<T> t;",
" for(auto &i:v){",
" auto it = lower_bound(all(t), i, cmp());",
" if(it == t.end()) t.push_back(i);",
" else *it = i;",
" }",
" return t.size();",
"}",
"template<typename T = int, typename cmp = less<T>>",
"vector<int> lis_recover(const vector<T> &v){",
" int n = v.size();",
" vector<T> t(n);",
" vector<int> idx(n), before(n);",
" int ans = 0;",
" for(int i=0;i<n;i++){",
" int loc = lower_bound(begin(t), begin(t) + ans, v[i], cmp()) - t.begin();",
" if(loc == ans) ++ans;",
" t[loc] = v[i]; idx[loc] = i;",
" before[i] = loc ? idx[loc - 1] : -1;",
" }",
" vector<int> res;",
" int cur = idx[ans - 1];",
" while(cur != -1){",
" res.push_back(v[cur]);",
" cur = before[cur];",
" }",
" reverse(all(res));",
" return res;",
"}"
],
"description": "lis"
},
"BigInt.cpp": {
"prefix": "BigInt",
"scope": "cpp",
"body": [
"// [Option 1] (Faster)",
"// test: http://boj.kr/12401ac7a9414dc8b96b8c2ebbf6eb49",
"// watch here: https://github.com/ngthanhtrung23/ACM_Notebook_new/blob/master/Math/bigint.h",
"",
"// [Option 2]",
"// test: http://boj.kr/43fa901846c44b828b13e8b4fa452579",
"// watch here: https://github.com/sercantutar/infint/blob/master/InfInt.h",
"",
"// NOTE:",
"// - Base 10^k. If need base 2^k, see submissions in:",
"// https://www.spoj.com/problems/PBBN2/ (>= 0 only, operations: *, power, xor)",
"// https://www.spoj.com/problems/PELLFOUR/ (see CPP, older submissions)",
"// https://codeforces.com/contest/504/submission/42348976 (with negative, several operations)",
"//",
"// Tested:",
"// - https://www.e-olymp.com/en/problems/266: Comparison",
"// - https://www.e-olymp.com/en/problems/267: Subtraction",
"// - https://www.e-olymp.com/en/problems/271: Multiplication",
"// - https://www.e-olymp.com/en/problems/272: Multiplication",
"// - https://www.e-olymp.com/en/problems/313: Addition",
"// - https://www.e-olymp.com/en/problems/314: Addition/Subtraction",
"// - https://www.e-olymp.com/en/problems/317: Multiplication (simple / karatsuba / fft)",
"// - https://www.e-olymp.com/en/problems/1327: Multiplication",
"// - https://www.e-olymp.com/en/problems/1328",
"// - VOJ BIGNUM: Addition, Subtraction, Multiplication.",
"// - SGU 111: sqrt",
"// - SGU 193",
"// - SPOJ MUL, VFMUL: Multiplication.",
"// - SPOJ FDIV, VFDIV: Division.",
"// - SPOJ SQRROOT: sqrt",
"",
"// BigInt {{{",
"const int BASE_DIGITS = 9;",
"const int BASE = 1000000000;",
"",
"struct BigInt {",
" int sign;",
" vector<int> a;",
"",
" // -------------------- Constructors --------------------",
" // Default constructor.",
" BigInt() : sign(1) {}",
"",
" // Constructor from long long.",
" BigInt(long long v) {",
" *this = v;",
" }",
" BigInt& operator = (long long v) {",
" sign = 1;",
" if (v < 0) {",
" sign = -1;",
" v = -v;",
" }",
" a.clear();",
" for (; v > 0; v = v / BASE)",
" a.push_back(v % BASE);",
" return *this;",
" }",
"",
" // Initialize from string.",
" BigInt(const string& s) {",
" read(s);",
" }",
"",
" // -------------------- Input / Output --------------------",
" void read(const string& s) {",
" sign = 1;",
" a.clear();",
" int pos = 0;",
" while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {",
" if (s[pos] == '-')",
" sign = -sign;",
" ++pos;",
" }",
" for (int i = s.size() - 1; i >= pos; i -= BASE_DIGITS) {",
" int x = 0;",
" for (int j = max(pos, i - BASE_DIGITS + 1); j <= i; j++)",
" x = x * 10 + s[j] - '0';",
" a.push_back(x);",
" }",
" trim();",
" }",
" friend istream& operator>>(istream &stream, BigInt &v) {",
" string s;",
" stream >> s;",
" v.read(s);",
" return stream;",
" }",
"",
" friend ostream& operator<<(ostream &stream, const BigInt &v) {",
" if (v.sign == -1 && !v.isZero())",
" stream << '-';",
" stream << (v.a.empty() ? 0 : v.a.back());",
" for (int i = (int) v.a.size() - 2; i >= 0; --i)",
" stream << setw(BASE_DIGITS) << setfill('0') << v.a[i];",
" return stream;",
" }",
"",
" // -------------------- Comparison --------------------",
" bool operator<(const BigInt &v) const {",
" if (sign != v.sign)",
" return sign < v.sign;",
" if (a.size() != v.a.size())",
" return a.size() * sign < v.a.size() * v.sign;",
" for (int i = ((int) a.size()) - 1; i >= 0; i--)",
" if (a[i] != v.a[i])",
" return a[i] * sign < v.a[i] * sign;",
" return false;",
" }",
"",
" bool operator>(const BigInt &v) const {",
" return v < *this;",
" }",
" bool operator<=(const BigInt &v) const {",
" return !(v < *this);",
" }",
" bool operator>=(const BigInt &v) const {",
" return !(*this < v);",
" }",
" bool operator==(const BigInt &v) const {",
" return !(*this < v) && !(v < *this);",
" }",
" bool operator!=(const BigInt &v) const {",
" return *this < v || v < *this;",
" }",
"",
" // Returns:",
" // 0 if |x| == |y|",
" // -1 if |x| < |y|",
" // 1 if |x| > |y|",
" friend int __compare_abs(const BigInt& x, const BigInt& y) {",
" if (x.a.size() != y.a.size()) {",
" return x.a.size() < y.a.size() ? -1 : 1;",
" }",
"",
" for (int i = ((int) x.a.size()) - 1; i >= 0; --i) {",
" if (x.a[i] != y.a[i]) {",
" return x.a[i] < y.a[i] ? -1 : 1;",
" }",
" }",
" return 0;",
" }",
"",
" // -------------------- Unary operator - and operators +- --------------------",
" BigInt operator-() const {",
" BigInt res = *this;",
" if (isZero()) return res;",
"",
" res.sign = -sign;",
" return res;",
" }",
"",
" // Note: sign ignored.",
" void __internal_add(const BigInt& v) {",
" if (a.size() < v.a.size()) {",
" a.resize(v.a.size(), 0);",
" }",
" for (int i = 0, carry = 0; i < (int) max(a.size(), v.a.size()) || carry; ++i) {",
" if (i == (int) a.size()) a.push_back(0);",
"",
" a[i] += carry + (i < (int) v.a.size() ? v.a[i] : 0);",
" carry = a[i] >= BASE;",
" if (carry) a[i] -= BASE;",
" }",
" }",
"",
" // Note: sign ignored.",
" void __internal_sub(const BigInt& v) {",
" for (int i = 0, carry = 0; i < (int) v.a.size() || carry; ++i) {",
" a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : 0);",
" carry = a[i] < 0;",
" if (carry) a[i] += BASE;",
" }",
" this->trim();",
" }",
"",
" BigInt operator += (const BigInt& v) {",
" if (sign == v.sign) {",
" __internal_add(v);",
" } else {",
" if (__compare_abs(*this, v) >= 0) {",
" __internal_sub(v);",
" } else {",
" BigInt vv = v;",
" swap(*this, vv);",
" __internal_sub(vv);",
" }",
" }",
" return *this;",
" }",
"",
" BigInt operator -= (const BigInt& v) {",
" if (sign == v.sign) {",
" if (__compare_abs(*this, v) >= 0) {",
" __internal_sub(v);",
" } else {",
" BigInt vv = v;",
" swap(*this, vv);",
" __internal_sub(vv);",
" this->sign = -this->sign;",
" }",
" } else {",
" __internal_add(v);",
" }",
" return *this;",
" }",
"",
" // Optimize operators + and - according to",
" // https://stackoverflow.com/questions/13166079/move-semantics-and-pass-by-rvalue-reference-in-overloaded-arithmetic",
" template< typename L, typename R >",
" typename std::enable_if<",
" std::is_convertible<L, BigInt>::value &&",
" std::is_convertible<R, BigInt>::value &&",
" std::is_lvalue_reference<R&&>::value,",
" BigInt>::type friend operator + (L&& l, R&& r) {",
" BigInt result(std::forward<L>(l));",
" result += r;",
" return result;",
" }",
" template< typename L, typename R >",
" typename std::enable_if<",
" std::is_convertible<L, BigInt>::value &&",
" std::is_convertible<R, BigInt>::value &&",
" std::is_rvalue_reference<R&&>::value,",
" BigInt>::type friend operator + (L&& l, R&& r) {",
" BigInt result(std::move(r));",
" result += l;",
" return result;",
" }",
"",
" template< typename L, typename R >",
" typename std::enable_if<",
" std::is_convertible<L, BigInt>::value &&",
" std::is_convertible<R, BigInt>::value,",
" BigInt>::type friend operator - (L&& l, R&& r) {",
" BigInt result(std::forward<L>(l));",
" result -= r;",
" return result;",
" }",
"",
" // -------------------- Operators * / % --------------------",
" friend pair<BigInt, BigInt> divmod(const BigInt& a1, const BigInt& b1) {",
" assert(b1 > 0); // divmod not well-defined for b < 0.",
"",
" long long norm = BASE / (b1.a.back() + 1);",
" BigInt a = a1.abs() * norm;",
" BigInt b = b1.abs() * norm;",
" BigInt q = 0, r = 0;",
" q.a.resize(a.a.size());",
"",
" for (int i = a.a.size() - 1; i >= 0; i--) {",
" r *= BASE;",
" r += a.a[i];",
" long long s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];",
" long long s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];",
" long long d = ((long long) BASE * s1 + s2) / b.a.back();",
" r -= b * d;",
" while (r < 0) {",
" r += b, --d;",
" }",
" q.a[i] = d;",
" }",
"",
" q.sign = a1.sign * b1.sign;",
" r.sign = a1.sign;",
" q.trim();",
" r.trim();",
" auto res = make_pair(q, r / norm);",
" if (res.second < 0) res.second += b1;",
" return res;",
" }",
" BigInt operator/(const BigInt &v) const {",
" if (v < 0) return divmod(-*this, -v).first;",
" return divmod(*this, v).first;",
" }",
"",
" BigInt operator%(const BigInt &v) const {",
" return divmod(*this, v).second;",
" }",
"",
" void operator/=(int v) {",
" assert(v > 0); // operator / not well-defined for v <= 0.",
" if (llabs(v) >= BASE) {",
" *this /= BigInt(v);",
" return ;",
" }",
" if (v < 0)",
" sign = -sign, v = -v;",
" for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {",
" long long cur = a[i] + rem * (long long) BASE;",
" a[i] = (int) (cur / v);",
" rem = (int) (cur % v);",
" }",
" trim();",
" }",
"",
" BigInt operator/(int v) const {",
" assert(v > 0); // operator / not well-defined for v <= 0.",
"",
" if (llabs(v) >= BASE) {",
" return *this / BigInt(v);",
" }",
" BigInt res = *this;",
" res /= v;",
" return res;",
" }",
" void operator/=(const BigInt &v) {",
" *this = *this / v;",
" }",
"",
" long long operator%(long long v) const {",
" assert(v > 0); // operator / not well-defined for v <= 0.",
" assert(v < BASE);",
" int m = 0;",
" for (int i = a.size() - 1; i >= 0; --i)",
" m = (a[i] + m * (long long) BASE) % v;",
" return m * sign;",
" }",
"",
" void operator*=(int v) {",
" if (llabs(v) >= BASE) {",
" *this *= BigInt(v);",
" return ;",
" }",
" if (v < 0)",
" sign = -sign, v = -v;",
" for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {",
" if (i == (int) a.size())",
" a.push_back(0);",
" long long cur = a[i] * (long long) v + carry;",
" carry = (int) (cur / BASE);",
" a[i] = (int) (cur % BASE);",
" //asm(\"divl %%ecx\" : \"=a\"(carry), \"=d\"(a[i]) : \"A\"(cur), \"c\"(base));",
" /*",
" int val;",
" __asm {",
" lea esi, cur",
" mov eax, [esi]",
" mov edx, [esi+4]",
" mov ecx, base",
" div ecx",
" mov carry, eax",
" mov val, edx;",
" }",
" a[i] = val;",
" */",
" }",
" trim();",
" }",
"",
" BigInt operator*(int v) const {",
" if (llabs(v) >= BASE) {",
" return *this * BigInt(v);",
" }",
" BigInt res = *this;",
" res *= v;",
" return res;",
" }",
"",
" // Convert BASE 10^old --> 10^new.",
" static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {",
" vector<long long> p(max(old_digits, new_digits) + 1);",
" p[0] = 1;",
" for (int i = 1; i < (int) p.size(); i++)",
" p[i] = p[i - 1] * 10;",
" vector<int> res;",
" long long cur = 0;",
" int cur_digits = 0;",
" for (int i = 0; i < (int) a.size(); i++) {",
" cur += a[i] * p[cur_digits];",
" cur_digits += old_digits;",
" while (cur_digits >= new_digits) {",
" res.push_back((long long)(cur % p[new_digits]));",
" cur /= p[new_digits];",
" cur_digits -= new_digits;",
" }",
" }",
" res.push_back((int) cur);",
" while (!res.empty() && !res.back())",
" res.pop_back();",
" return res;",
" }",
"",
" void fft(vector<complex<double> > &x, bool invert) const {",
" int n = (int) x.size();",
"",
" for (int i = 1, j = 0; i < n; ++i) {",
" int bit = n >> 1;",
" for (; j >= bit; bit >>= 1)",
" j -= bit;",
" j += bit;",
" if (i < j)",
" swap(x[i], x[j]);",
" }",
"",
" for (int len = 2; len <= n; len <<= 1) {",
" double ang = 2 * 3.14159265358979323846 / len * (invert ? -1 : 1);",
" complex<double> wlen(cos(ang), sin(ang));",
" for (int i = 0; i < n; i += len) {",
" complex<double> w(1);",
" for (int j = 0; j < len / 2; ++j) {",
" complex<double> u = x[i + j];",
" complex<double> v = x[i + j + len / 2] * w;",
" x[i + j] = u + v;",
" x[i + j + len / 2] = u - v;",
" w *= wlen;",
" }",
" }",
" }",
" if (invert)",
" for (int i = 0; i < n; ++i)",
" x[i] /= n;",
" }",
"",
" void multiply_fft(const vector<int> &x, const vector<int> &y, vector<int> &res) const {",
" vector<complex<double> > fa(x.begin(), x.end());",
" vector<complex<double> > fb(y.begin(), y.end());",
" int n = 1;",
" while (n < (int) max(x.size(), y.size()))",
" n <<= 1;",
" n <<= 1;",
" fa.resize(n);",
" fb.resize(n);",
"",
" fft(fa, false);",
" fft(fb, false);",
" for (int i = 0; i < n; ++i)",
" fa[i] *= fb[i];",
" fft(fa, true);",
"",
" res.resize(n);",
" long long carry = 0;",
" for (int i = 0; i < n; ++i) {",
" long long t = (long long) (fa[i].real() + 0.5) + carry;",
" carry = t / 1000;",
" res[i] = t % 1000;",
" }",
" }",
"",
" BigInt mul_simple(const BigInt &v) const {",
" BigInt res;",
" res.sign = sign * v.sign;",
" res.a.resize(a.size() + v.a.size());",
" for (int i = 0; i < (int) a.size(); ++i)",
" if (a[i])",
" for (int j = 0, carry = 0; j < (int) v.a.size() || carry; ++j) {",
" long long cur = res.a[i + j] + (long long) a[i] * (j < (int) v.a.size() ? v.a[j] : 0) + carry;",
" carry = (int) (cur / BASE);",
" res.a[i + j] = (int) (cur % BASE);",
" }",
" res.trim();",
" return res;",
" }",
"",
" typedef vector<long long> vll;",
"",
" static vll karatsubaMultiply(const vll &a, const vll &b) {",
" int n = a.size();",
" vll res(n + n);",
" if (n <= 32) {",
" for (int i = 0; i < n; i++)",
" for (int j = 0; j < n; j++)",
" res[i + j] += a[i] * b[j];",
" return res;",
" }",
"",
" int k = n >> 1;",
" vll a1(a.begin(), a.begin() + k);",
" vll a2(a.begin() + k, a.end());",
" vll b1(b.begin(), b.begin() + k);",
" vll b2(b.begin() + k, b.end());",
"",
" vll a1b1 = karatsubaMultiply(a1, b1);",
" vll a2b2 = karatsubaMultiply(a2, b2);",
"",
" for (int i = 0; i < k; i++)",
" a2[i] += a1[i];",
" for (int i = 0; i < k; i++)",
" b2[i] += b1[i];",
"",
" vll r = karatsubaMultiply(a2, b2);",
" for (int i = 0; i < (int) a1b1.size(); i++)",
" r[i] -= a1b1[i];",
" for (int i = 0; i < (int) a2b2.size(); i++)",
" r[i] -= a2b2[i];",
"",
" for (int i = 0; i < (int) r.size(); i++)",
" res[i + k] += r[i];",
" for (int i = 0; i < (int) a1b1.size(); i++)",
" res[i] += a1b1[i];",
" for (int i = 0; i < (int) a2b2.size(); i++)",
" res[i + n] += a2b2[i];",
" return res;",
" }",
"",
" BigInt mul_karatsuba(const BigInt &v) const {",
" vector<int> x6 = convert_base(this->a, BASE_DIGITS, 6);",
" vector<int> y6 = convert_base(v.a, BASE_DIGITS, 6);",
" vll x(x6.begin(), x6.end());",
" vll y(y6.begin(), y6.end());",
" while (x.size() < y.size())",
" x.push_back(0);",
" while (y.size() < x.size())",
" y.push_back(0);",
" while (x.size() & (x.size() - 1))",
" x.push_back(0), y.push_back(0);",
" vll c = karatsubaMultiply(x, y);",
" BigInt res;",
" res.sign = sign * v.sign;",
" long long carry = 0;",
" for (int i = 0; i < (int) c.size(); i++) {",
" long long cur = c[i] + carry;",
" res.a.push_back((int) (cur % 1000000));",
" carry = cur / 1000000;",
" }",
" res.a = convert_base(res.a, 6, BASE_DIGITS);",
" res.trim();",
" return res;",
" }",
"",
" void operator*=(const BigInt &v) {",
" *this = *this * v;",
" }",
" BigInt operator*(const BigInt &v) const {",
" if (a.size() * v.a.size() <= 1000111) return mul_simple(v);",
" if (a.size() > 500111 || v.a.size() > 500111) return mul_fft(v);",
" return mul_karatsuba(v);",
" }",
"",
" BigInt mul_fft(const BigInt& v) const {",
" BigInt res;",
" res.sign = sign * v.sign;",
" multiply_fft(convert_base(a, BASE_DIGITS, 3), convert_base(v.a, BASE_DIGITS, 3), res.a);",
" res.a = convert_base(res.a, 3, BASE_DIGITS);",
" res.trim();",
" return res;",
" }",
"",
" // -------------------- Misc --------------------",
" BigInt abs() const {",
" BigInt res = *this;",
" res.sign *= res.sign;",
" return res;",
" }",
" void trim() {",
" while (!a.empty() && !a.back())",
" a.pop_back();",
" if (a.empty())",
" sign = 1;",
" }",
"",
" bool isZero() const {",
" return a.empty() || (a.size() == 1 && !a[0]);",
" }",
"",
" friend BigInt gcd(const BigInt &x, const BigInt &y) {",
" return y.isZero() ? x : gcd(y, x % y);",
" }",
" friend BigInt lcm(const BigInt &x, const BigInt &y) {",
" return x / gcd(x, y) * y;",
" }",
"",
" friend BigInt sqrt(const BigInt &a1) {",
" BigInt a = a1;",
" while (a.a.empty() || a.a.size() % 2 == 1)",
" a.a.push_back(0);",
"",
" int n = a.a.size();",
"",
" int firstDigit = (int) sqrt((double) a.a[n - 1] * BASE + a.a[n - 2]);",
" int norm = BASE / (firstDigit + 1);",
" a *= norm;",
" a *= norm;",
" while (a.a.empty() || a.a.size() % 2 == 1)",
" a.a.push_back(0);",
"",
" BigInt r = (long long) a.a[n - 1] * BASE + a.a[n - 2];",
" firstDigit = (int) sqrt((double) a.a[n - 1] * BASE + a.a[n - 2]);",
" int q = firstDigit;",
" BigInt res;",
"",
" for(int j = n / 2 - 1; j >= 0; j--) {",
" for(; ; --q) {",
" BigInt r1 = (r - (res * 2 * BigInt(BASE) + q) * q) * BigInt(BASE) * BigInt(BASE) + (j > 0 ? (long long) a.a[2 * j - 1] * BASE + a.a[2 * j - 2] : 0);",
" if (r1 >= 0) {",
" r = r1;",
" break;",
" }",
" }",
" res *= BASE;",
" res += q;",
"",
" if (j > 0) {",
" int d1 = res.a.size() + 2 < r.a.size() ? r.a[res.a.size() + 2] : 0;",
" int d2 = res.a.size() + 1 < r.a.size() ? r.a[res.a.size() + 1] : 0;",
" int d3 = res.a.size() < r.a.size() ? r.a[res.a.size()] : 0;",
" q = ((long long) d1 * BASE * BASE + (long long) d2 * BASE + d3) / (firstDigit * 2);",
" }",
" }",
"",
" res.trim();",
" return res / norm;",
" }",
"};",
"// }}}"
],
"description": "BigInt"
},
"bisect.cpp": {
"prefix": "bisect",
"scope": "cpp",
"body": [
"int lo = $1, hi = $2, mid;",
"// chk(lo) = true, chk(hi) = false",
"auto chk = [&](auto x)->bool{",
" $0",
"};",
"while(lo + 1 < hi) chk(mid = midpoint(lo, hi)) ? lo = mid : hi = mid;"
],
"description": "bisect"
},
"Compresser.cpp": {
"prefix": "Compresser",
"scope": "cpp",
"body": [
"// reference: https://sotanishy.github.io/cp-library-cpp/misc/compress.hpp.html",
"template<class T>",
"struct Compresser{",
" vector<T> t;",
" Compresser() = default;",
" Compresser(const vector<T> &v): t(v){}",
" void add(const auto&... x){ ((t.push_back(x)), ...); }",
" void compress(){",
" ranges::sort(t);",
" t.erase(begin(ranges::unique(t)), end(t));",
" }",
" int floor(const T& x) const{",
" return ranges::distance(begin(t), ranges::upper_bound(t, x)) - 1;",
" }",
" int ceil(const T& x) const{",
" return compress(x);",
" }",
" int compress(const T& x) const {",
" return ranges::distance(begin(t), ranges::lower_bound(t, x));",
" }",
" void compress(vector<T> &v) const {",
" ranges::transform(v, begin(v), [&](T x){return compress(x);});",
" }",
" const T& val(int i) const { return t[i]; }",
" int size() const{ return t.size(); }",
"};"
],
"description": "Compresser"
},
"countInversion.cpp": {
"prefix": "countInversion",
"scope": "cpp",
"body": [
"/*",
"countInversion: number of swap operations to make nums from original",
"O(n log n)",
"*/",
"// put BIT(fenwick tree) struct here",
"ll countInversion(const vector<int>&original,const vector<int>&nums){",
" assert(original.size() == nums.size());",
" int n = nums.size();",
" vector<int> t(original);",
" sort(all(t)); t.erase(unique(all(t)), t.end());",
" int nn = t.size();",
" vector<vector<int>> v(nn);",
" for(int i=0;i<n;i++)",
" v[lower_bound(all(t), original[i]) - t.begin()].push_back(i);",
" BIT f(n+1);",
" ll ans = 0;",
" for(int i=n-1;i>=0;i--){",
" int x = lower_bound(all(t), nums[i]) - t.begin();",
" int idx = v[x].back(); v[x].pop_back();",
" ++idx; // 1-base",
" ans += f.qry(idx - 1);",
" f.upd(idx, 1);",
" }",
" return ans;",
"}"
],
"description": "countInversion"
},
"Date.cpp": {
"prefix": "Date",
"scope": "cpp",
"body": [
"// src: 2011 Rocky Mountain Regional Contest H",
"struct Date {",
" int month, day, year;",
" Date(int mm = 1, int dd = 1, int yy = 1900) : month(mm), day(dd), year(yy) {}",
" static constexpr int daysInMonth[2][13] = {",
" { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },",
" { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }",
" };",
" static constexpr bool isLeap(int y){return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);}",
" bool operator<(const Date& date) const {",
" if (year != date.year) return year < date.year;",
" if (month != date.month) return month < date.month;",
" return day < date.day;",
" }",
" bool operator <=(const Date& date) const{",
" return not (date < *this);",
" }",
" bool operator >(const Date& date) const{",
" return date < *this;",
" }",
" bool operator >=(const Date& date) const{",
" return not (*this < date);",
" }",
" void inc() {",
" if (day++ == daysInMonth[isLeap(year)][month]) {",
" day = 1;",
" if (month++ == 12) {",
" month = 1;",
" year++;",
" }",
" }",
" }",
" void dec() {",
" if (day-- == 1) {",
" if (month-- == 1) {",
" month = 12;",
" assert(year > 0);",
" year--;",
" }",
" day = daysInMonth[isLeap(year)][month];",
" }",
" }",
" void change(int step){",
" if(step == 0) return;",
" if(step > 0){",
" for(int i=0;i<step;i++) inc();",
" }else{",
" step *= -1;",
" for(int i=0;i<step;i++) dec();",
" }",
" }",
" friend istream& operator>>(istream& is, Date& date) {",
" char c;",
" return is >> date.month >> c >> date.day >> c >> date.year;",
" }",
" friend ostream& operator<<(ostream& os, const Date& date) {",
" return os << date.month << '/' << date.day << '/' << date.year;",
" }",
"};"
],
"description": "Date"
},
"DisjointSet.cpp": {
"prefix": "DisjointSet",
"scope": "cpp",
"body": [
"struct DisjointSet{",
" int n;",
" vector<int> par; // negative: size of set, otherwise: parent",
" // additional info here",
" DisjointSet(int _n = 0) : n(_n), par(_n, -1) {}",
" void make_leader(int u){par[u] = -1;}",
" int leader(int u) {",
" assert(0 <= u and u < n);",
" // return par[u] < 0 : u : leader(par[u]); // no path compression",
" return par[u] < 0 ? u : par[u] = leader(par[u]);",
" }",
" bool merge(int u, int v) {",
" u = leader(u); v = leader(v);",
" if (u == v) return false;",
" if ((-par[u]) < (-par[v])) swap(u, v); // make sz[u] >= sz[v]",
" // S.push_back({u, v, par[v]}); // rollback info",
" par[u] += par[v];",
" par[v] = u;",
" // do smth here",
" return true;",
" }",
" bool same(int u, int v) {",
" return leader(u) == leader(v);",
" }",
" int leader_size(int u){",
" return -par[u];",
" }",
" int size(int u){",
" return -leader_size(leader(u));",
" }",
" // don't do this with make_leader, or it will be bugged.",
" vector<vector<int>> groups(){",
" vector res(n, vector<int>());",
" for(int i=0;i<n;i++)",
" if(par[i] < 0)",
" res[i].reserve(-par[i]);",
" for(int i=0;i<n;i++)",
" res[leader(i)].push_back(i);",
" erase_if(res, [](vector<int>&v){return empty(v);});",
" return res;",
" }",
"",
" // vector<array<int, 3>> S; // rollback stack",
" // void rollback(int cnt = 1){",
" // for(int i=0;i<cnt;i++){",
" // assert(not empty(S));",
" // auto [u,v,psz] = S.back(); S.pop_back();",
" // par[u] -= psz; par[v] = psz;",
" // }",
" // }",
"};"
],
"description": "DisjointSet"
},
"erasable_heap.cpp": {
"prefix": "erasable_heap",
"scope": "cpp",
"body": [
"/** Eraseable heap",
" * cmp\ub294 bool operator()\uc774 \uc815\uc758\ub41c struct \ub610\ub294 class",
" * cmp \uc0c1\uc73c\ub85c \uac00\uc7a5 \ud070 \uac83\uc774 \ubc18\ud658\ub428",
" */",
"template<class T, class cmp = less<T>>",
"struct heap{",
" priority_queue<T, vector<T>, cmp> iq, dq;",
" cmp f;",
" bool eq(const T&l, const T&r){return not f(l, r) and not f(r, l);}",
" void push(T x){iq.push(x);}",
" void del(T x){dq.push(x);}",
" void upd(){",
" while(not dq.empty()){",
" if(iq.empty() or f(iq.top(), dq.top())) dq.pop();",
" else{",
" if(eq(iq.top(), dq.top())) iq.pop(), dq.pop();",
" else break;",
" }",
" }",
" }",
" bool empty(){upd(); return iq.empty();}",
" T pop(){upd(); T x = iq.top(); iq.pop(); return x;}",
" T top(){upd(); return iq.top();}",
"};"
],
"description": "erasable_heap"
},
"IntervalContainer.cpp": {
"prefix": "IntervalContainer",
"scope": "cpp",
"body": [
"// source: https://github.com/kth-competitive-programming/kactl/blob/main/content/various/IntervalContainer.h",
"",
"// Interval: [L, R)",
"class IntervalSet : public set<pii> {",
"public:",
"\tusing set<pii>::set; // \uc0dd\uc131\uc790 \uc0c1\uc18d",
"",
"\tset<pii>::iterator add(int L, int R) {",
"\t\tif (L == R) return end();",
"\t\tauto it = lower_bound({L, R}), before = it;",
"\t\t// not necessary starts",
"\t\tif(it != end() and it->first == L) return it;",
"\t\tif(it != begin() and prev(it)->second >= R) return it;",
"\t\t// not necessary ends",
"\t\twhile (it != end() && it->first <= R) {",
"\t\t\tR = max(R, it->second);",
"\t\t\tbefore = it = erase(it);",
"\t\t}",
"\t\tif (it != begin() && (--it)->second >= L) {",
"\t\t\tL = min(L, it->first);",
"\t\t\tR = max(R, it->second);",
"\t\t\terase(it);",
"\t\t}",
"\t\treturn insert(before, {L, R});",
"\t}",
"",
"\tvoid remove(int L, int R) {",
"\t\tif (L == R) return;",
"\t\tauto it = add(L, R);",
"\t\tauto r2 = it->second;",
"\t\tif (it->first == L) erase(it);",
"\t\telse (int&)it->second = L;",
"\t\tif (R != r2) emplace(R, r2);",
"\t}",
"};"
],
"description": "IntervalContainer"
},
"LineContainer.cpp": {
"prefix": "LineContainer",
"scope": "cpp",
"body": [
"/*",
"Modified Line container for dynamic CHT",
"y = kx + m",
"qry: gives minimum value for given x, upper hull",
"find bool operator< (const Line& o) to change minimum / maximum properties.",
"for minimum: k > o.k, x->m > y->m",
"for maximum: k < o.k, x->m < y->m",
"for doubles, change all (ll -> double), (inf -> 1/.0), (div(a,b) = a/b)",
"reference: https://github.com/kth-competitive-programming/kactl/blob/main/content/data-structures/LineContainer.h",
"*/",
"#define MIN_VALUE",
"struct Line{",
" mutable ll k, m, p;",
" // int cnt; // additional info",
" #ifdef MIN_VALUE",
" bool operator<(const Line& o) const{return k!=o.k?k>o.k:m>o.m;}",
" #else",
" bool operator<(const Line& o) const{return k!=o.k?k<o.k:m<o.m;}",
" #endif",
" bool operator<(ll x) const {return p < x;}",
" ll f(ll x) const{return k * x + m;}",
"};",
"struct LC : multiset<Line, less<>> {",
" void print_it(iterator it){fprintf(stderr, \"[%d] %lld %lld %lld\\n\",(int)distance(begin(),it), it->k, it->m, it->p);}",
" void print(){",
" fprintf(stderr, \"size: %d\\n\", (int)size());",
" for(auto it = begin(); it != end(); it++)",
" print_it(it);",
" }",
" static const ll inf = numeric_limits<ll>::max();",
" inline ll div(ll a, ll b){ // floored division",
" return a / b - ((a^b)<0 && a%b);",
" }",
" bool apply(iterator x, iterator y){",
" assert(x != end() && next(x) == y);",
" if(y == end()) return x->p = inf, false;",
" if(x->k == y->k) x->p = (*y<*x?inf:-inf);",
" else x->p = div(y->m - x->m, x->k - y->k);",
" return x->p >= y->p;",
" }",
" bool add(const Line &t){",
" auto y = insert(t), z = next(y), x = y;",
" if(z!=end() && y->k == z->k) return erase(y), false;",
" while(apply(y, z)) z = erase(z);",
" if(x != begin() && apply(--x, y)) return apply(x, y = erase(y)), false;",
" while((y=x) != begin() && (--x)->p >= y->p) apply(x, erase(y));",
" return true;",
" }",
" Line qry(ll x){",
" assert(!empty());",
" auto l = lower_bound(x); assert(l != end());",
" return *l;",
" }",
"};"
],
"description": "LineContainer"
},
"persistent_lefitist_heap.cpp": {
"prefix": "persistent_lefitist_heap",
"scope": "cpp",
"body": [
"/**",
" * Title: \"Min\" Persistent Leftist Heap",
" * Repository: https://github.com/Pentagon03/Algorithms/blob/master/Data%20Structure/persistent_leftist_heap.cpp",
" * Concept: https://en.wikipedia.org/wiki/Leftist_tree",
" * Code Reference: nor( https://judge.yosupo.jp/submission/87297 )",
" */",
"template <typename Key, typename Value>",
"struct PersistentLeftistHeap {",
" using self_t = PersistentLeftistHeap<Key, Value>;",