-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpe_poly_base
2579 lines (2330 loc) · 78.2 KB
/
pe_poly_base
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
#ifndef PE_POLY_BASE_
#define PE_POLY_BASE_
#include "pe_base"
#include "pe_int128"
#include "pe_type_traits"
#include "pe_bit"
#include "pe_mod"
#include "pe_nt"
#include "pe_poly_base_common"
// Each of the following included files contains a third party polynomial
// operation function implementations. It is NOT necessary that each file should
// implement every function.
#include "pe_poly_base_flint"
#include "pe_poly_base_ntl"
#include "pe_poly_base_min25"
#include "pe_poly_base_libbf"
// This file is the pe library's polynomial operation implementation.
//
// This file will decide whether to use third party library implementations.
// Polynomial multiplication
//
// It has different polynomial multiplication implementations.
//
// Naming conventions:
// [implementation namespace ::]PolyMul[implementation suffix][scalability
// suffix]
//
// implementation namespace is one of:
// ntt32 (32 bit internal mod)
// ntt64 (64 bit internal mod)
// min25
// libbf
// flint
// ntl
//
// implementation suffix:
// Dc: divide and conquer
//
// scalability suffix:
// SmallMod: mod is a small
// LargeMod: mod is a large
// Small: (mod-1)^2*n is relative small
// Large: (mod-1)^2*n is relative large
//
// Template parameters:
// T: native integer which can be promoted to uint64
//
// Availabilities:
// PolyMulDc : always available
// ntt32::PolyMulSmall : always available
// ntt32::PolyMulLarge : always available
// ntt64::PolyMulSmall : always available
// ntt64::PolyMulLarge : always available
// min25::PolyMulSmall : requires int128
// min25::PolyMulLarge : requires int128
// libbf::PolyMul : enable libbf && LIMB_BITS >= 64
// flint::pmod::PolyMul : enable flint && GMP_LIMB_BITS >= 64
// flint::PolyMul : enable flint && GMP_LIMB_BITS >= 64
// ntl::PolyMulSmallMod : enable ntl
// ntl::PolyMulLargeMod : enable ntl
// ntl::PolyMul : enable ntl
//
// ntt32::PolyMul = ntt32::PolyMulLarge
// ntt64::PolyMul = ntt64::PolyMulLarge
// min25::PolyMul = min25::PolyMulLarge
// ntl::PolyMul = if mod is small then ntl::PolyMulSmallMod
// else ntl::PolyMulLargeMod
//
// PolyMul will choose an implementation from the aboves.
//
// Constraints:
// mod < 2^62 in all implementation.
//
// ntt32::PolyMulSmall
// (mod-1)^2*n < 4593671624212873217 4.5e18
//
// ntt32::PolyMulLarge
// (mod-1)^2*n < 14797252050511790781119856641 1.4e28
//
// ntt64::PolyMulSmall
// (mod-1)^2*n < 1945555039024054273 1.9e18
//
// ntt64::PolyMulLarge
// (mod-1)^2*n < 350480037951100867051507526341230593 3.5e35
//
// min25::PolyMulSmall
// (mod-1)^2*n < 1128298388379402241 1.1e18
//
// min25::PolyMulLarge
// (mod-1)^2*n < 1265198875113262859862934516672757761 1.2e36
//
// libbf::PolyMul
// (mod-1)^2*n < 340282366920938463463374607431768211456 (2^128) 3.4e38
//
// flint::pmod::PolyMul
// mod is a word size prime
//
// flint::PolyMul
// mod is a word size number
//
// ntl::PolyMulSmallMod
// mod < 2^30 if sizeof(long) = 4
// mod < 2^50 if sizeof(long) = 8
//
// ntl::PolyMulLargeMod
// no constraints
namespace pe {
namespace ntt_base {
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
NttTrans(T* data, const int64 n) {
int64 j = n >> 1;
for (int64 i = 1; i < n - 1; ++i) {
if (i < j) std::swap(data[i], data[j]);
int64 k = n >> 1;
while (j >= k) {
j -= k;
k >>= 1;
}
if (j < k) j += k;
}
}
} // namespace ntt_base
// mod is 32 bit
namespace ntt32 {
#define HAS_POLY_MUL_NTT32 1
struct NttMod32 {
// mod = r * 2 ^ k + 1
// mod is prime
const unsigned mod;
const unsigned r;
const int k;
const unsigned g;
unsigned omg[32];
mutable unsigned* pre_omg[32];
NttMod32(unsigned mod, unsigned r, int k, unsigned g)
: mod(mod), r(r), k(k), g(g) {
for (int i = 0; i <= k; ++i) {
omg[i] = static_cast<unsigned>(
PowerMod<uint64, uint64, uint64>(g, (mod - 1) >> i, mod));
}
std::fill(pre_omg, pre_omg + 32, nullptr);
}
~NttMod32() {
for (int i = 0; i <= k; ++i) {
if (pre_omg[i] != nullptr) {
delete[] pre_omg[i];
pre_omg[i] = nullptr;
}
}
}
void InitPreOmg(int K) const {
PE_ASSERT(K <= k);
for (int i = 0; i <= K; ++i) {
if (pre_omg[i] != nullptr) {
continue;
}
const int64 cnt = 1LL << i;
pre_omg[i] = new unsigned[cnt];
auto* target = pre_omg[i];
const uint64 m = omg[i];
unsigned last = 1;
target[0] = 1;
for (int64 i = 1; i < cnt; ++i) {
last = last * m % mod;
target[i] = last;
}
}
}
};
static const NttMod32 ntt_mod_1(2013265921ULL, 15ULL, 27, 31ULL);
static const NttMod32 ntt_mod_2(2281701377ULL, 17ULL, 27, 3ULL);
static const NttMod32 ntt_mod_3(3221225473ULL, 3ULL, 30, 5ULL);
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
Ntt(T* data, const int64 n, const NttMod32& moder, bool inv = false) {
ntt_base::NttTrans(data, n);
const auto mod = moder.mod;
int id = 0;
for (int64 h = 2; h <= n; h <<= 1) {
const auto* pre_omg = moder.pre_omg[++id];
if (pre_omg) {
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 16) if (n / h > 100000)
#endif
for (int64 j = 0; j < n; j += h) {
const auto* omg = pre_omg;
const int64 half_h = h >> 1;
const int64 limit = j + half_h;
for (int64 k = j; k < limit; ++k) {
const auto u = data[k];
const auto t =
MulMod(static_cast<uint64>(*omg++), data[k + half_h], mod);
data[k] = AddMod(u, t, mod);
data[k + half_h] = SubMod(u, t, mod);
}
}
} else {
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 16) if (n / h > 100000)
#endif
for (int64 j = 0; j < n; j += h) {
uint64 omgn = 1;
const int64 half_h = h >> 1;
const int64 limit = j + half_h;
for (int64 k = j; k < limit; ++k) {
const auto u = data[k];
const auto t = MulMod(omgn, data[k + half_h], mod);
data[k] = AddMod(u, t, mod);
data[k + half_h] = SubMod(u, t, mod);
omgn = MulMod(omgn, moder.omg[id], mod);
}
}
}
}
if (inv) {
const int64 half_n = n >> 1;
for (int64 i = 1; i < half_n; ++i) std::swap(data[i], data[n - i]);
const uint64 c = PowerMod<uint64>(n, mod - 2, mod);
for (int64 i = 0; i < n; ++i) data[i] = c * data[i] % mod;
}
}
void InitNtt(int k = 22) {
PE_ASSERT(k <= 27 && k >= 0);
ntt_mod_1.InitPreOmg(k);
ntt_mod_2.InitPreOmg(k);
ntt_mod_3.InitPreOmg(k);
}
// The small version uses two modulus.
struct NttSmallConstant {
static constexpr uint64 M1 = 2013265921;
static constexpr uint64 M2 = 2281701377;
static constexpr uint64 INV_M1__M2 = 1140850697;
};
// Multiply two polynomials.
// Make sure the length of result is at least: n + m - 1
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyMulSmallImpl(const T* X, int64 n, const T* Y, int64 m, T* result,
int64 mod) {
static_assert(pe_is_unsigned_v<T>, "T must be unsigned");
const int64 aligned_size = 1LL << pe_lgll(2 * (n + m - 1) - 1);
// TODO(baihacker): decide the size automatically.
const NttMod32* moder_list[2] = {&ntt_mod_1, &ntt_mod_2};
std::vector<uint64> tresult[2];
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 1) num_threads(2)
#endif
for (int id = 0; id < 2; ++id) {
const NttMod32& moder = *moder_list[id];
const auto tmod = moder.mod;
const bool no_mod = static_cast<uint64>(mod) <= static_cast<uint64>(tmod);
std::vector<uint64> XX(aligned_size);
std::vector<uint64> YY(aligned_size);
if (no_mod) {
for (int64 i = 0; i < n; ++i) XX[i] = X[i];
} else {
for (int64 i = 0; i < n; ++i) XX[i] = Mod(X[i], tmod);
}
for (int64 i = n; i < aligned_size; ++i) XX[i] = 0;
if (no_mod) {
for (int64 i = 0; i < m; ++i) YY[i] = Y[i];
} else {
for (int64 i = 0; i < m; ++i) YY[i] = Mod(Y[i], tmod);
}
for (int64 i = m; i < aligned_size; ++i) YY[i] = 0;
#if ENABLE_OPENMP
#pragma omp parallel sections if (n + m >= 100000)
#endif
{
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&XX[0], aligned_size, moder);
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&YY[0], aligned_size, moder);
}
const uint64 mod = moder.mod;
for (int64 i = 0; i < aligned_size; ++i) {
XX[i] = static_cast<uint64>(XX[i]) * YY[i] % mod;
}
Ntt(&XX[0], aligned_size, moder, true);
tresult[id] = std::move(XX);
}
const int64 result_size = n + m - 1;
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 100000) if (n + m >= 100000)
#endif
for (int64 i = 0; i < result_size; ++i) {
const uint64 a = tresult[0][i];
const uint64 b = tresult[1][i];
const uint64 x = b >= a ? b - a : b + NttSmallConstant::M2 - a;
const uint64 y = x * NttSmallConstant::INV_M1__M2 % NttSmallConstant::M2;
const uint64 t = y * NttSmallConstant::M1 + a;
result[i] = mod > 0 ? t % mod : t;
}
}
// Multiply two polynomials.
// The length of result is at least n + m - 1.
POLY_MUL_IMPL(PolyMulSmall, PolyMulSmallImpl)
#define NTT32_DIRECT_INT128_IMPLEMENTATION 0
struct NttConstant {
static constexpr uint64 M1 = 2013265921;
static constexpr uint64 M2 = 2281701377;
static constexpr uint64 M3 = 3221225473;
#if PE_HAS_INT128 && NTT32_DIRECT_INT128_IMPLEMENTATION
static constexpr uint64 M12 = M1 * M2;
static constexpr uint64 M13 = M1 * M3;
static constexpr uint64 M23 = M2 * M3;
static constexpr uint64 IM12 = 2300875347;
static constexpr uint64 IM13 = 1792765347;
static constexpr uint64 IM23 = 1006632973;
static constexpr uint128 M13M = (uint128)IM13 * M13;
static constexpr uint128 M23M = (uint128)IM23 * M23;
static constexpr uint128 M12M = (uint128)IM12 * M12;
static constexpr uint128 MMM = (uint128)M1 * M2 * M3;
#else
static constexpr uint64 INV_M1__M2 = 1140850697;
static constexpr uint64 M1M2 = M1 * M2;
static constexpr uint64 INV_M3__M1M2 = 1312477593879670191ULL;
#endif
};
// Multiply two polynomials.
// Make sure the length of result is at least: n + m - 1
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyMulLargeImpl(const T* X, int64 n, const T* Y, int64 m, T* result,
int64 mod) {
static_assert(pe_is_unsigned_v<T>, "T must be unsigned");
const int64 aligned_size = 1LL << pe_lgll(2 * (n + m - 1) - 1);
// TODO(baihacker): decide the size automatically.
const NttMod32* moder_list[3] = {&ntt_mod_1, &ntt_mod_2, &ntt_mod_3};
std::vector<uint64> tresult[3];
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 1) num_threads(3)
#endif
for (int id = 0; id < 3; ++id) {
const NttMod32& moder = *moder_list[id];
const auto tmod = moder.mod;
const bool no_mod = static_cast<uint64>(mod) <= static_cast<uint64>(tmod);
std::vector<uint64> XX(aligned_size);
std::vector<uint64> YY(aligned_size);
if (no_mod) {
for (int64 i = 0; i < n; ++i) XX[i] = X[i];
} else {
for (int64 i = 0; i < n; ++i) XX[i] = Mod(X[i], tmod);
}
for (int64 i = n; i < aligned_size; ++i) XX[i] = 0;
if (no_mod) {
for (int64 i = 0; i < m; ++i) YY[i] = Y[i];
} else {
for (int64 i = 0; i < m; ++i) YY[i] = Mod(Y[i], tmod);
}
for (int64 i = m; i < aligned_size; ++i) YY[i] = 0;
#if ENABLE_OPENMP
#pragma omp parallel sections if (n + m >= 100000)
#endif
{
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&XX[0], aligned_size, moder);
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&YY[0], aligned_size, moder);
}
const uint64 mod = moder.mod;
for (int64 i = 0; i < aligned_size; ++i) {
XX[i] = static_cast<uint64>(XX[i]) * YY[i] % mod;
}
Ntt(&XX[0], aligned_size, moder, true);
tresult[id] = std::move(XX);
}
const int64 result_size = n + m - 1;
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 100000) if (n + m >= 100000)
#endif
for (int64 i = 0; i < result_size; ++i) {
#if PE_HAS_INT128 && NTT32_DIRECT_INT128_IMPLEMENTATION
const uint128 a = tresult[0][i] * NttConstant::M23M;
const uint128 b = tresult[1][i] * NttConstant::M13M;
const uint128 c = tresult[2][i] * NttConstant::M12M;
const uint128 t = a + b + c;
const auto tmp = t < NttConstant::MMM ? t : t % NttConstant::MMM;
result[i] = mod > 0 ? tmp % mod : tmp;
#else
const uint64 a = tresult[0][i];
const uint64 b = tresult[1][i];
const uint64 c = tresult[2][i];
const uint64 x1 = b >= a ? b - a : NttConstant::M2 - a + b;
const uint64 y1 = x1 * NttConstant::INV_M1__M2 % NttConstant::M2;
const uint64 modab = y1 * NttConstant::M1 + a;
const uint64 x2 = modab >= c ? modab - c : NttConstant::M1M2 - c + modab;
#if PE_HAS_INT128
const uint64 y2 = static_cast<uint128>(x2) * NttConstant::INV_M3__M1M2 %
NttConstant::M1M2;
const uint128 t = static_cast<uint128>(y2) * NttConstant::M3 + c;
result[i] = mod > 0 ? t % mod : t;
#else
PE_ASSERT(mod > 0);
const uint64 y2 = MulMod(x2, NttConstant::INV_M3__M1M2, NttConstant::M1M2);
const uint64 t = MulMod(y2 % mod, NttConstant::M3 % mod, mod);
result[i] = AddMod(t, c % mod, mod);
#endif
#endif
}
}
// Multiply two polynomials.
// The length of result is at least n + m - 1.
POLY_MUL_IMPL(PolyMulLarge, PolyMulLargeImpl)
} // namespace ntt32
// mod is 64 bit
namespace ntt64 {
#define HAS_POLY_MUL_NTT64 1
struct NttMod64 {
// mod = r * 2 ^ k + 1
// mod is prime
const uint64 mod;
const unsigned r;
const int k;
const unsigned g;
uint64 omg[64];
mutable uint64* pre_omg[64];
NttMod64(uint64 mod, unsigned r, int k, unsigned g)
: mod(mod), r(r), k(k), g(g) {
for (int i = 0; i <= k; ++i) {
omg[i] = PowerMod<uint64, uint64, uint64>(g, (mod - 1) >> i, mod);
}
std::fill(pre_omg, pre_omg + 64, nullptr);
}
~NttMod64() {
for (int i = 0; i <= k; ++i) {
if (pre_omg[i] != nullptr) {
delete[] pre_omg[i];
pre_omg[i] = nullptr;
}
}
}
void InitPreOmg(int K) const {
PE_ASSERT(K <= k);
for (int i = 0; i <= K; ++i) {
if (pre_omg[i] != nullptr) {
continue;
}
const int64 cnt = 1LL << i;
pre_omg[i] = new uint64[cnt];
auto* target = pre_omg[i];
const uint64 m = omg[i];
uint64 last = 1;
target[0] = 1;
for (int64 i = 1; i < cnt; ++i) {
#if PE_HAS_INT128
last = Uint128ModUint64(static_cast<uint128>(last) * m, mod);
#else
last = MulMod(last, m, mod);
#endif
target[i] = last;
}
}
}
};
static const NttMod64 ntt_mod_1(180143985094819841ULL, 5ULL, 55, 6ULL);
static const NttMod64 ntt_mod_2(1945555039024054273ULL, 27ULL, 56, 5ULL);
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
Ntt(T* data, const int64 n, const NttMod64& moder, bool inv = false) {
ntt_base::NttTrans(data, n);
const auto mod = moder.mod;
int id = 0;
for (int64 h = 2; h <= n; h <<= 1) {
const auto* pre_omg = moder.pre_omg[++id];
if (pre_omg) {
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 16) if (n / h > 100000)
#endif
for (int64 j = 0; j < n; j += h) {
const auto* omg = pre_omg;
const int64 half_h = h >> 1;
const int64 limit = j + half_h;
for (int64 k = j; k < limit; ++k) {
const auto u = data[k];
#if PE_HAS_INT128
const auto t = Uint128ModUint64(
static_cast<uint128>(*omg++) * data[k + half_h], mod);
#else
const auto t = MulMod(*omg++, data[k + half_h], mod);
#endif
data[k] = AddMod(u, t, mod);
data[k + half_h] = SubMod(u, t, mod);
}
}
} else {
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 16) if (n / h > 100000)
#endif
for (int64 j = 0; j < n; j += h) {
uint64 omgn = 1;
const int64 half_h = h >> 1;
const int64 limit = j + half_h;
for (int64 k = j; k < limit; ++k) {
const auto u = data[k];
#if PE_HAS_INT128
const auto t = Uint128ModUint64(
static_cast<uint128>(omgn) * data[k + half_h], mod);
#else
const auto t = MulMod(omgn, data[k + half_h], mod);
#endif
data[k] = AddMod(u, t, mod);
data[k + half_h] = SubMod(u, t, mod);
#if PE_HAS_INT128
omgn =
Uint128ModUint64(static_cast<uint128>(omgn) * moder.omg[id], mod);
#else
omgn = MulMod(omgn, moder.omg[id], mod);
#endif
}
}
}
}
if (inv) {
const int64 half_n = n >> 1;
for (int64 i = 1; i < half_n; ++i) std::swap(data[i], data[n - i]);
const uint64 c = PowerMod<uint64>(n, mod - 2, mod);
for (int64 i = 0; i < n; ++i) {
#if PE_HAS_INT128
data[i] = Uint128ModUint64(static_cast<uint128>(c) * data[i], mod);
#else
data[i] = MulMod(c, data[i], mod);
#endif
}
}
}
void InitNtt(int k = 22) {
PE_ASSERT(k <= 30 && k >= 0);
ntt_mod_1.InitPreOmg(k);
ntt_mod_2.InitPreOmg(k);
}
// Multiply two polynomials.
// Make sure the length of result is at least: n + m - 1
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyMulSmallImpl(const T* X, int64 n, const T* Y, int64 m, T* result,
int64 mod) {
static_assert(pe_is_unsigned_v<T>, "T must be unsigned");
const int64 aligned_size = 1LL << pe_lgll(2 * (n + m - 1) - 1);
// TODO(baihacker): decide the size automatically.
const NttMod64* moder_list[1] = {&ntt_mod_2};
std::vector<uint64> tresult[1];
for (int id = 0; id < 1; ++id) {
const NttMod64& moder = *moder_list[id];
const auto tmod = moder.mod;
const bool no_mod = static_cast<uint64>(mod) <= static_cast<uint64>(tmod);
std::vector<uint64> XX(aligned_size);
std::vector<uint64> YY(aligned_size);
if (no_mod) {
for (int64 i = 0; i < n; ++i) XX[i] = X[i];
} else {
for (int64 i = 0; i < n; ++i) XX[i] = Mod(X[i], tmod);
}
for (int64 i = n; i < aligned_size; ++i) XX[i] = 0;
if (no_mod) {
for (int64 i = 0; i < m; ++i) YY[i] = Y[i];
} else {
for (int64 i = 0; i < m; ++i) YY[i] = Mod(Y[i], tmod);
}
for (int64 i = m; i < aligned_size; ++i) YY[i] = 0;
#if ENABLE_OPENMP
#pragma omp parallel sections if (n + m >= 100000)
#endif
{
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&XX[0], aligned_size, moder);
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&YY[0], aligned_size, moder);
}
const uint64 mod = moder.mod;
for (int64 i = 0; i < aligned_size; ++i) {
#if PE_HAS_INT128
XX[i] = Uint128ModUint64(static_cast<uint128>(XX[i]) * YY[i], mod);
#else
XX[i] = MulMod(XX[i], YY[i], mod);
#endif
}
Ntt(&XX[0], aligned_size, moder, true);
tresult[id] = std::move(XX);
}
const int64 result_size = n + m - 1;
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 100000) if (n + m >= 100000)
#endif
for (int64 i = 0; i < result_size; ++i) {
const uint64 a = tresult[0][i];
result[i] = mod > 0 ? a % mod : a;
}
}
// Multiply two polynomials.
// The length of result is at least n + m - 1.
POLY_MUL_IMPL(PolyMulSmall, PolyMulSmallImpl)
struct NttConstant {
static constexpr uint64 M1 = 180143985094819841ULL;
static constexpr uint64 M2 = 1945555039024054273ULL;
static constexpr uint64 INV_M1__M2 = 714693687804754632ULL;
};
// Multiply two polynomials.
// Make sure the length of result is at least: n + m - 1
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyMulLargeImpl(const T* X, int64 n, const T* Y, int64 m, T* result,
int64 mod) {
static_assert(pe_is_unsigned_v<T>, "T must be unsigned");
const int64 aligned_size = 1LL << pe_lgll(2 * (n + m - 1) - 1);
// TODO(baihacker): decide the size automatically.
const NttMod64* moder_list[2] = {&ntt_mod_1, &ntt_mod_2};
std::vector<uint64> tresult[2];
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 1) num_threads(2)
#endif
for (int id = 0; id < 2; ++id) {
const NttMod64& moder = *moder_list[id];
const auto tmod = moder.mod;
const bool no_mod = static_cast<uint64>(mod) <= static_cast<uint64>(tmod);
std::vector<uint64> XX(aligned_size);
std::vector<uint64> YY(aligned_size);
if (no_mod) {
for (int64 i = 0; i < n; ++i) XX[i] = X[i];
} else {
for (int64 i = 0; i < n; ++i) XX[i] = Mod(X[i], tmod);
}
for (int64 i = n; i < aligned_size; ++i) XX[i] = 0;
if (no_mod) {
for (int64 i = 0; i < m; ++i) YY[i] = Y[i];
} else {
for (int64 i = 0; i < m; ++i) YY[i] = Mod(Y[i], tmod);
}
for (int64 i = m; i < aligned_size; ++i) YY[i] = 0;
#if ENABLE_OPENMP
#pragma omp parallel sections if (n + m >= 100000)
#endif
{
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&XX[0], aligned_size, moder);
#if ENABLE_OPENMP
#pragma omp section
#endif
Ntt(&YY[0], aligned_size, moder);
}
const uint64 mod = moder.mod;
for (int64 i = 0; i < aligned_size; ++i) {
#if PE_HAS_INT128
XX[i] = Uint128ModUint64(static_cast<uint128>(XX[i]) * YY[i], mod);
#else
XX[i] = MulMod(XX[i], YY[i], mod);
#endif
}
Ntt(&XX[0], aligned_size, moder, true);
tresult[id] = std::move(XX);
}
const int64 result_size = n + m - 1;
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 100000) if (n + m >= 100000)
#endif
for (int64 i = 0; i < result_size; ++i) {
const uint64 a = tresult[0][i];
const uint64 b = tresult[1][i];
const uint64 x = b >= a ? b - a : b + NttConstant::M2 - a;
#if PE_HAS_INT128
const uint64 y = Uint128ModUint64(
static_cast<uint128>(x) * NttConstant::INV_M1__M2, NttConstant::M2);
const uint128 t = static_cast<uint128>(y) * NttConstant::M1 + a;
result[i] = mod > 0 ? t % mod : t;
#else
PE_ASSERT(mod > 0);
const uint64 y = MulMod(x, NttConstant::INV_M1__M2, NttConstant::M2);
const uint64 t = MulMod(y % mod, NttConstant::M1 % mod, mod);
result[i] = AddMod(t, a % mod, mod);
#endif
}
}
// Multiply two polynomials.
// The length of result is at least n + m - 1.
POLY_MUL_IMPL(PolyMulLarge, PolyMulLargeImpl)
} // namespace ntt64
template <typename T>
std::vector<T> PolyShift(const std::vector<T>& p, int64 m) {
if (m == 0) {
return p;
}
if (m > 0) {
std::vector<T> ret(std::size(p) + m);
for (int64 i = static_cast<int64>(std::size(p)) + m - 1; i >= m; --i) {
ret[i] = p[i - m];
}
for (int64 i = 0; i < m; ++i) {
ret[i] = 0;
}
return ret;
} else {
if (m >= static_cast<int64>(std::size(p))) {
return {0};
}
const int64 new_size = static_cast<int64>(std::size(p)) - m;
std::vector<T> ret(new_size);
for (int i = 0; i < new_size; ++i) {
ret[i] = p[i + m];
}
return ret;
}
}
template <typename T>
std::vector<T> PolyShiftLeft(const std::vector<T>& p, int64 m) {
return PolyShift(p, m);
}
template <typename T>
std::vector<T> PolyShiftRight(const std::vector<T>& p, int64 m) {
return PolyShift(p, -m);
}
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyAdd(const T* X, const int64 n, const T* Y, const int64 m, T* result,
int64 mod) {
if (n <= m) {
for (int64 i = 0; i < n; ++i) {
result[i] = AddMod(X[i], Y[i], mod);
}
std::copy(Y + n, Y + m, result + n);
} else {
for (int64 i = 0; i < m; ++i) {
result[i] = AddMod(X[i], Y[i], mod);
}
std::copy(X + m, X + n, result + m);
}
}
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(std::vector<T>)
PolyAdd(const std::vector<T>& X, const std::vector<T>& Y, int64 mod) {
const int64 n = static_cast<int64>(std::size(X));
const int64 m = static_cast<int64>(std::size(Y));
std::vector<T> result(std::max(n, m));
PolyAdd(&X[0], n, &Y[0], m, &result[0], mod);
return result;
}
template <typename T>
SL void PolyAdd(const T* X, const int64 n, const T* Y, const int64 m,
T* result) {
if (n <= m) {
for (int64 i = 0; i < n; ++i) {
result[i] = X[i] + Y[i];
}
std::copy(Y + n, Y + m, result + n);
} else {
for (int64 i = 0; i < m; ++i) {
result[i] = X[i] + Y[i];
}
std::copy(X + m, X + n, result + m);
}
}
template <typename T>
SL std::vector<T> PolyAdd(const std::vector<T>& X, const std::vector<T>& Y) {
const int64 n = static_cast<int64>(std::size(X));
const int64 m = static_cast<int64>(std::size(Y));
std::vector<T> result(std::max(n, m));
PolyAdd(&X[0], n, &Y[0], m, &result[0]);
return result;
}
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolySub(const T* X, const int64 n, const T* Y, const int64 m, T* result,
int64 mod) {
if (n <= m) {
for (int64 i = 0; i < n; ++i) {
result[i] = SubMod(X[i], Y[i], mod);
}
for (int64 i = n; i < m; ++i) {
result[i] = Y[i] == 0 ? 0 : mod - Y[i];
}
} else {
for (int64 i = 0; i < m; ++i) {
result[i] = SubMod(X[i], Y[i], mod);
}
std::copy(X + m, X + n, result + m);
}
}
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(std::vector<T>)
PolySub(const std::vector<T>& X, const std::vector<T>& Y, int64 mod) {
const int64 n = static_cast<int64>(std::size(X));
const int64 m = static_cast<int64>(std::size(Y));
std::vector<T> result(std::max(n, m));
PolySub(&X[0], n, &Y[0], m, &result[0], mod);
return result;
}
template <typename T>
SL void PolySub(const T* X, const int64 n, const T* Y, const int64 m,
T* result) {
if (n <= m) {
for (int64 i = 0; i < n; ++i) {
result[i] = X[i] - Y[i];
}
for (int64 i = n; i < m; ++i) {
result[i] = -Y[i];
}
} else {
for (int64 i = 0; i < m; ++i) {
result[i] = X[i] - Y[i];
}
std::copy(X + m, X + n, result + m);
}
}
template <typename T>
SL std::vector<T> PolySub(const std::vector<T>& X, const std::vector<T>& Y) {
const int64 n = static_cast<int64>(std::size(X));
const int64 m = static_cast<int64>(std::size(Y));
std::vector<T> result(std::max(n, m));
PolySub(&X[0], n, &Y[0], m, &result[0]);
return result;
}
namespace internal {
// Multiplies two polynomials of the same length using Divide and Conquer
// algorithm. size result >= 2 * n size return = 2 * n (deg return = 2 * n - 1)
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyMulDcImpl(const T* X, const T* Y, const int64 n, T* result, int64 mod) {
static_assert(pe_is_unsigned_v<T>, "T must be unsigned");
const int64 n2 = n << 1;
if (n <= 49) {
std::fill(result, result + n2, 0);
for (int64 i = 0; i < n; ++i) {
for (int64 j = 0; j < n; ++j) {
result[i + j] = AddMod(result[i + j], MulMod(X[i], Y[j], mod), mod);
}
}
return;
}
const int64 m1 = (n + 1) >> 1;
const int64 m0 = n - m1;
const int64 dbm1 = m1 << 1;
const int64 dbm0 = m0 << 1;
// m1 >= m0
const T* x0 = X + m1;
const T* y0 = Y + m1;
const T* x1 = X;
const T* y1 = Y;
T* x0y0 = new T[dbm1];
T* x1y1 = new T[dbm1];
#if ENABLE_OPENMP
#pragma omp parallel sections if (n > 5000)
#endif
{
#if ENABLE_OPENMP
#pragma omp section
#endif
PolyMulDcImpl(x0, y0, m0, x0y0, mod);
#if ENABLE_OPENMP
#pragma omp section
#endif
PolyMulDcImpl(x1, y1, m1, x1y1, mod);
}
if (m0 != m1) {
x0y0[dbm0] = 0;
x0y0[dbm0 + 1] = 0;
}
T* w = new T[dbm1];
{
T* u = new T[m1];
T* v = new T[m1];
for (int64 i = 0; i < m0; ++i) {
u[i] = AddMod(x0[i], x1[i], mod), v[i] = AddMod(y0[i], y1[i], mod);
}
if (m0 != m1) {
u[m1 - 1] = x1[m1 - 1];
v[m1 - 1] = y1[m1 - 1];
}
PolyMulDcImpl(u, v, m1, w, mod);
delete[] u;
delete[] v;
for (int64 i = 0; i < m1 * 2; ++i) {
w[i] = SubMod(w[i], AddMod(x0y0[i], x1y1[i], mod), mod);
}
}
std::fill(result, result + n2, 0);
for (int64 i = 0; i < dbm0; ++i) {
result[dbm1 + i] = AddMod(result[dbm1 + i], x0y0[i], mod);
}
for (int64 i = 0; i < dbm1; ++i) {
result[m1 + i] = AddMod(result[m1 + i], w[i], mod);
}
for (int64 i = 0; i < dbm1; ++i) {
result[i] = AddMod(result[i], x1y1[i], mod);
}
delete[] x0y0;
delete[] x1y1;
delete[] w;
}
} // namespace internal
// Multiplies two polynomials using Divide and Conquer algorithm.
// size result >= n + m - 1
template <typename T>
SL REQUIRES((is_builtin_integer_v<T>)) RETURN(void)
PolyMulDc(const T* X, const int64 n, const T* Y, const int64 m, T* result,
int64 mod) {
using UnsignedT = typename pe_make_unsigned<T>::type;
if (n == m) {
// In case n == m, we still allocate tempory result space to meet the
// requirement of PolyMulDcImpl.
std::vector<T> tresult(2 * n);
internal::PolyMulDcImpl<UnsignedT>(
reinterpret_cast<const UnsignedT*>(X),
reinterpret_cast<const UnsignedT*>(Y), n,
reinterpret_cast<UnsignedT*>(&tresult[0]), mod);
std::copy(tresult.begin(), tresult.begin() + n + m - 1, result);
} else if (n > m) {
std::vector<T> YY(n);
for (int64 i = 0; i < m; ++i) YY[i] = Y[i];