-
Notifications
You must be signed in to change notification settings - Fork 1
/
computational.h
4047 lines (4002 loc) · 121 KB
/
computational.h
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 COMPUTATIONAL_H
#define COMPUTATIONAL_H
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include <typeinfo>
#include<functional>
#include<cstdlib>
#include<complex>
#include<algorithm>
#include<deque>
#include<thread>
#include <iterator>
#include<tuple>
#include<cstdint>
//#include<execution>
#include<utility>
using std::cerr;
using std::complex;
using std::cout;
using std::deque;
using std::get;
using std::ios;
using std::ostream;
using std::pair;
using std::ref;
using std::string;
using std::swap;
using std::tuple;
//using std::thread;
using std::vector;
namespace malg{
//-----常量部分-----
template<class DC> const DC Pi = 3.1415926535897932;
const double Machine_Epsilon = pow(2, -52);
template <class DC>
const std::function<DC(DC)> Identity =[](DC x) { return x; };
template<class DC>
const std::function<DC(DC, DC)> Multiply = [](DC x, DC y) { return x * y; };
//-----声明部分-----
//杂例I
template <class DM>
double Norm(const vector<DM> &, double);
template<class DJ> DJ Abs(DJ);
template <class DJ> DJ Sign(DJ);
template <class DJ> vector<DJ> Range(DJ, DJ,DJ);
template <class DJ> vector<DJ> Range(DJ, DJ);
template <class DJ> vector<DJ> Rand(int);
template <class DJ> DJ Limit(std::function<DJ(DJ)>, DJ);
template <class DJ> DJ N(DJ,int);
inline int Sig(){return rand()%2?1:-1;}//random Sign(+/-)
//大整数类
class BigInt
{
public:
BigInt cutzero();
BigInt();
BigInt(const string&);
BigInt(const char *);
BigInt(int);
BigInt(bool);
BigInt(const string &, bool);
friend BigInt operator-(const BigInt &);
friend bool operator==(const BigInt&, const BigInt&);
friend bool operator!=(const BigInt &, const BigInt &);
friend bool operator<(const BigInt&, const BigInt&);
friend bool operator>(const BigInt&, const BigInt&);
friend bool operator<=(const BigInt&, const BigInt&);
friend bool operator>=(const BigInt&, const BigInt&);
friend ostream &operator << (ostream &, const BigInt&);
friend BigInt operator>>(const BigInt&, int);
friend BigInt operator+(const BigInt &,const BigInt&);
friend BigInt operator-(const BigInt &,const BigInt&);
friend BigInt operator*(const BigInt &,const BigInt&);
friend BigInt operator/(const BigInt&, const BigInt&);
friend BigInt operator^(const BigInt&, int);
friend BigInt operator^(const BigInt&,unsigned long long);
BigInt &operator+=(const BigInt &);
BigInt &operator-=(const BigInt &);
explicit operator int();
friend BigInt Product_NTT(const BigInt &, const BigInt &);
friend BigInt Product_DivideConquer(const BigInt &, const BigInt &);
void Reassign(string );
void _digitChange(string);
void _signChange(bool);
friend string Get_digit(const BigInt&);
friend string GetString(const BigInt &);
friend bool PositivityTest(const BigInt&);
friend unsigned Length(const BigInt &);
private:
string _digit;
bool _sign;
};
vector<unsigned long long> CompressBit(const string&, unsigned int);
class HighPrecision: public BigInt
{
public:
HighPrecision CutTail();
HighPrecision();
HighPrecision(string);
HighPrecision(const char[]);
explicit HighPrecision(const BigInt&);
HighPrecision(double);
HighPrecision(string, int);
HighPrecision(double, int);
friend ostream &operator<<(ostream &, const HighPrecision &);
friend HighPrecision operator<<(const HighPrecision &,int);
friend HighPrecision operator+(const HighPrecision &, const HighPrecision &);
friend HighPrecision operator- (const HighPrecision &);
friend HighPrecision operator-(const HighPrecision &, const HighPrecision &);
friend HighPrecision operator*(const HighPrecision &, const HighPrecision &);
friend HighPrecision operator/(const HighPrecision &, const HighPrecision &);
friend unsigned DecimalLength(const HighPrecision &);
friend BigInt IntegerPart(const HighPrecision &);
friend HighPrecision DecimalPart(const HighPrecision &);
friend int Order(const HighPrecision &);
friend string TopKDigit(const HighPrecision &,unsigned);
friend unsigned TotalLength(const HighPrecision &);
friend unsigned SignificantLength(const HighPrecision &);
friend HighPrecision SignificantFigure(const HighPrecision &, unsigned);
private:
string _decimal;
};
//多项式类
template<class DC>
class Polynomial{
public:
Polynomial();
Polynomial(const vector<DC>&);
Polynomial(const vector<DC>&, int);
Polynomial(DC);
Polynomial<DC> &operator=(const Polynomial<DC>&);
DC operator()(DC);
template <class DD> friend Polynomial<DD> operator+(const Polynomial<DD> &,const Polynomial<DD> &);
template <class DD> friend Polynomial<DD> operator-(const Polynomial<DD> &,const Polynomial<DD> &);
template <class DD> friend Polynomial<DD> operator+(DD, const Polynomial<DD>&);
template <class DD> friend Polynomial<DD> operator*(DD, const Polynomial<DD>&);
template <class DD> friend Polynomial<DD> operator*(const Polynomial<DD>&,const Polynomial<DD>&);
template <class DD> friend Polynomial<DD> operator/(const Polynomial<DD>&,const Polynomial<DD>&);
template <class DD> friend Polynomial<DD> operator^(const Polynomial<DD>&,int);
template<class DD> friend ostream &operator << (ostream &,const Polynomial<DD> &);
template <class DD> friend Polynomial<DD> operator>>(const Polynomial<DD>&, int);
Polynomial<DC> &operator+=(const Polynomial<DC>&);
Polynomial<DC> &operator-=(const Polynomial<DC>&);
template <class DD> friend DD Get(const Polynomial<DD> &, DD);
template <class DD> friend vector<DD> GetCoef(const Polynomial<DD> &);
template <class DD> friend void Disp(const Polynomial<DD>&);
template <class DD> friend int Deg(const Polynomial<DD>&);
template <class DD> friend Polynomial<DD> SubPoly(const Polynomial<DD>&, int, int);
template <class DD> friend Polynomial<DD> D(const Polynomial<DD>&);
template <class DD> friend Polynomial<DD> Integrate(const Polynomial<DD> &);
template <class DD> friend DD Integrate(const Polynomial<DD> &,DD,DD);
template <class DD> friend Polynomial<DD> Schmidt(int,DD,DD);
template <class DD> friend Polynomial<DD> Legendre(int n);
private:
int poly_deg;
vector<DC> poly_coeff;
};
Polynomial<double> operator*(const Polynomial<double>&,const Polynomial<double>&);
Polynomial<float> operator*(const Polynomial<float>&,const Polynomial<float>&);
Polynomial<complex<double>> operator*(const Polynomial<complex<double>> &, const Polynomial<complex<double>> &);
Polynomial<complex<float>> operator*(const Polynomial<complex<float>> &, const Polynomial<complex<float>> &);
/*Polynomial<unsigned> operator*(const Polynomial<unsigned> &, const Polynomial<unsigned> &);
Polynomial<unsigned long long> operator*(const Polynomial<unsigned long long> &, const Polynomial<unsigned long long> &);*/ /*test*/
//矩阵类
template<class DM>
class Matrix
{
public:
Matrix();
Matrix(DM);
Matrix(int,int);
Matrix(const vector<vector<DM>>&, int, int);
Matrix(const vector<vector<DM>>&);
Matrix(const vector<vector<DM>> &, const char[]);
Matrix<DM> &operator=(const Matrix<DM> &);
DM &operator()(int,int);
DM &operator()(int);
const vector<DM> &operator[](int);
template <class DB> friend bool operator==(const Matrix<DB> &, const Matrix<DB> &);
template<class DB> friend ostream &operator<< (ostream &,const Matrix<DB> &);
template<class DB> friend Matrix<DB> operator+(const Matrix<DB> &,const Matrix<DB>&);
template<class DB> friend Matrix<DB> operator-(const Matrix<DB> &,const Matrix<DB>&);
template <class DB>friend Matrix<DB> operator-(const Matrix<DB> &);
template<class DB> friend Matrix<DB> operator*(const Matrix<DB> &,const Matrix<DB>&);
template<class DB> friend Matrix<DB> operator/(const Matrix<DB> &,const Matrix<DB>&);
template<class DB> friend Matrix<DB> operator^(const Matrix<DB> &,int);
template <class DB> friend Matrix<DB> operator+(DB, const Matrix<DB>&);
template <class DB> friend Matrix<DB> operator+(const Matrix<DB>&,DB);
template <class DB> friend Matrix<DB> operator-(DB, const Matrix<DB>&);
template <class DB> friend Matrix<DB> operator-(const Matrix<DB>&,DB);
template <class DB> friend Matrix<DB> operator*(DB, const Matrix<DB>&);
template <class DB> friend Matrix<DB> operator*(const Matrix<DB>&,DB);
template<class DB> friend Matrix<DB> operator/(const Matrix<DB> &,DB);
Matrix<DM> &operator+=(const Matrix<DM> &);
Matrix<DM> &operator-=(const Matrix<DM> &);
template <class DB> friend DB Get(const Matrix<DB> &, int, int);
template <class DB> friend void Make(Matrix<DB> &, int, int, DB);
template<class DB> friend void Clear(Matrix<DB>&);
template<class DB> friend void Disp(const Matrix<DB>&);
template <class DB> friend Matrix<DB> Generate(int, int);
template <class DB> friend Matrix<DB> Act(std::function<DB(DB)>, const Matrix<DB> &);
template <class DB> friend Matrix<DB> Act(std::function<DB(DB, DB)>, const Matrix<DB> &, const Matrix<DB> &);
template <class DB> friend Matrix<DB> Table(std::function<DB(DB)>, const vector<DB>&);
template<class DB> friend Matrix<DB> Transpose(const Matrix<DB>&);
template<class DB> friend vector<DB> Diagonal(const Matrix<DB>&);
template <class DB> friend Matrix<DB> DiagonalMatrix(const vector<DB> &);
template <class DB> friend DB Min(const Matrix<DB>&);
template <class DB> friend DB Max(const Matrix<DB>&);
template <class DB> friend DB ColumnMin(const Matrix<DB> &,int );
template <class DB> friend DB ColumnMax(const Matrix<DB> &,int);
template<class DB> friend int RowSize(const Matrix<DB>&);
template<class DB> friend int ColumnSize(const Matrix<DB>&);
template <class DB> friend void Resize(Matrix<DB> &,int,int);
template <class DB> friend Matrix<DB> SubRow(const Matrix<DB> &, int, int);
template <class DB> friend Matrix<DB> SubColumn(const Matrix<DB> &, int, int);
template <class DB> friend Matrix<DB> SubMatrix(const Matrix<DB> &, int, int, int, int);
template <class DB> friend void ReplaceMatrix(Matrix<DB> &, int, int, int, int, const Matrix<DB> &);
template <class DB> friend void DeleteRow(Matrix<DB> &,int n);
template <class DB> friend void ReplaceRow(Matrix<DB> &, int n,const Matrix<DB>&);
//解线性方程组
template<class DB> friend Matrix<DB> RowCat(const Matrix<DB>&, const Matrix<DB>&);
template <class DB> friend Matrix<DB> RowCat(const vector<Matrix<DB>>&);
template <class DB> friend Matrix<DB> ColumnCat(const Matrix<DB> &, const Matrix<DB> &);
template<class DB> friend void SwapRow(Matrix<DB>&,int,int);
template <class DB> friend Matrix<DB> USplit(const Matrix<DB>&);
template <class DB> friend vector<Matrix<DB>> GaussianElimination(const Matrix<DB>&,const Matrix<DB>&);
template <class DB> friend Matrix<DB> LUCompactDecomposition(const Matrix<DB>&);
template <class DB> friend Matrix<DB> LUPCompactDecomposition(const Matrix<DB>&, vector<int> &, int&, DB&);
template <class DB> friend Matrix<DB> GaussianElimination(const Matrix<DB>&);
template <class DB> friend int MatrixRank(const Matrix<DB>&);
template <class DB> friend DB Det(const Matrix<DB>&);
template <class DB> friend vector<Matrix<DB>> LUDecomposition(const Matrix<DB>&);
template <class DB> friend vector<Matrix<DB>> LUPDecomposition(const Matrix<DB> &);
template <class DB> friend vector<vector<DB>> Tridiagonal(const Matrix<DB>&);
template <class DB> friend vector<vector<DB>> TridiagonalSeparation(const vector<vector<DB>>&);
template <class DB> friend Matrix<DB> CholeskyCompactDecomposition(const Matrix<DB>&);
template <class DB> friend vector<Matrix<DB>> CholeskyDecomposition(const Matrix<DB> &);
template <class DB> friend vector<Matrix<DB>> JacobiIterationMatrix(const Matrix<DB>&, const Matrix<DB>&);
template <class DB> friend vector<Matrix<DB>> GSIterationMatrix(const Matrix<DB>&, const Matrix<DB>&);
template <class DB> friend Matrix<DB> LUSolve(const Matrix<DB>&,const Matrix<DB>&);
template <class DB> friend Matrix<DB> LUPSolve(const Matrix<DB>&,const vector<int>&,const Matrix<DB>&);
template <class DB> friend Matrix<DB> LSolve(const Matrix<DB>&,const Matrix<DB>&);
template <class DB> friend Matrix<DB> USolve(const Matrix<DB>&,const Matrix<DB>&);
template <class DB> friend Matrix<DB> TridiagonalSolve(const vector<vector<DB>>&, const Matrix<DB>&);
template <class DB> friend Matrix<DB> Iteration(const Matrix<DB>&, const Matrix<DB>&);
template<class DB> friend Matrix<DB> LinearSolve(const Matrix<DB>&,const Matrix<DB>&,string,double);
template<class DB> friend Matrix<DB> LinearSolve(const Matrix<DB>&,const Matrix<DB>&,string);
template<class DB> friend Matrix<DB> LinearSolve(const Matrix<DB>&,const Matrix<DB>&);
//求逆
template<class DB> friend Matrix<DB> Eye(int);
template<class DB> friend Matrix<DB> Inverse(const Matrix<DB>&);
//范数、条件数
template <class DB> friend DB Trace(const Matrix<DB>&);
template <class DB> friend DB VectorNorm(const Matrix<DB> &, double);
template <class DB> friend DB Norm(const Matrix<DB>&, double);
template <class DB> friend DB Norm(const Matrix<DB>&, string);
template <class DB> friend double Cond(const Matrix<DB>&, double);
template <class DB> friend double Cond(const Matrix<DB>&, string);
//插值
template <class DB> friend vector<std::function<DB(DB)>> LagrangeBasis(const Matrix<DB> &);
template <class DB> friend DB LBasisToInterp(const vector<std::function<DB(DB)>>&,const Matrix<DB> &,DB);
template <class DB> friend Matrix<DB> DifferenceQuotientTable(const Matrix<DB> &);
template <class DB> friend Matrix<DB> DividedDifferenceTable(const Matrix<DB> &);
template <class DB> friend DB DifferenceQuotient(const Matrix<DB> &);
template <class DB> friend DB DifferenceQuotient(std::function<DB(DB)>, const Matrix<DB> &);
template <class DB> friend DB DQTableToInterp(const Matrix<DB> &, const Matrix<DB> &,DB);
template <class DB> friend DB DDTableToInterp(const Matrix<DB> &, const Matrix<DB> &,DB);
template <class DB> friend DB _2P3DHermiteInterp(const Matrix<DB> &,int i,int j,DB);
template <class DB> friend DB _3P3DHermiteInterp(const Matrix<DB> &,DB);
template <class DE> friend Matrix<DE> SplineSlope(const Matrix<DE> &);
template <class DE> friend DE Interpolation(const Matrix<DE> &,DE,string);
template <class DE> friend DE Interpolation(const Matrix<DE> &,DE);
template <class DE> friend Matrix<DE> Interpolation(const Matrix<DE> &A, const Matrix<DE> &x, string);
template <class DE> friend std::function<DE(DE)> Interpolation(const Matrix<DE> &,string);
//拟合与逼近
template <class DB> friend Matrix<DB> PseudoInverse(const Matrix<DB>&);
template <class DB> friend Matrix<DB> LeastSquares(const Matrix<DB>&, const Matrix<DB>&);
template <class DB> friend Polynomial<DB> PolyFit(const Matrix<DB> &, const vector<Polynomial<DB>> &);
template <class DB> friend Polynomial<DB> PolyFit(const Matrix<DB>&,int);
template <class DB> friend Polynomial<DB> PolyFit(std::function<DB(DB)>,const vector<Polynomial<DB>>&,DB,DB);
private:
vector<vector<DM>> value;
int row_num;
int column_num;
};
template <class DB> const Matrix<DB> EmptyMatrix = Matrix<DB>(0, 0);
//图像绘制
template <class DG>
class Graphics
{
public:
Graphics();
Graphics(const Matrix<bool> &, const std::pair<DG, DG> &, const std::pair<DG, DG> &);
void show();
private:
Matrix<bool> pixel;
std::pair<DG,DG> xrange;
std::pair<DG,DG> yrange;
};
//积分
template <class DI> DI Integrate(std::function<DI(DI)>,DI,DI,string);
template <class DI> DI Integrate(std::function<DI(DI)>,DI,DI);
//微分
template <class DD> DD D(std::function<DD(DD)>, DD);
//一元函数求根
template <class DD> DD Iteration(std::function<DD(DD)>, DD);
template<class DD> DD FindRoot(std::function<DD(DD)>, DD,DD);
template <class DD> DD FindRoot(std::function<DD(DD)>, DD,string);
template <class DD> DD FindRoot(std::function<DD(DD)>, DD);
template <class DD> DD FindRoot(std::function<DD(DD)>, DD,string,int);
template <class DD> DD Sqrt(DD);
template <> double Sqrt(double);
//杂例II
BigInt Factorial(int);
BigInt Fibonacci_r(int);
BigInt Fibonacci_p(int);
BigInt Fibonacci_m(int);
BigInt Fibonacci(int);
//-----定义部分-----
//快速傅里叶变换
inline uint32_t Rev(uint32_t k,uint8_t m)
{
uint32_t s = 0;
while(m>0)
{
--m;
s += ((k%2) << m);
k = (k >> 1);
}
return s;
}
template<class DC> vector<DC> bit_reverse_copy(const vector<DC> &a)
{
uint32_t n = a.size();
uint8_t m = log2(n);
vector<DC> A(n);
for(uint32_t k=0;k<n;++k)
A[Rev(k,m)] = a[k];
return A;
}
template<class DC> vector<complex<DC>> iterative_FFT(const vector<complex<DC>>&a,bool _jud)
{
vector<complex<DC>> A = bit_reverse_copy(a);
int n = a.size();
int m= 1;
complex<DC> omega_m;
complex<DC> omega;
for (int s = 1; s <= log2(n);++s)
{
m= m<< 1;
if(_jud)
omega_m=complex<DC>(cos(2*Pi<DC>/m),sin(2*Pi<DC>/m));
else
omega_m=complex<DC>(cos(2*Pi<DC>/m),0-sin(2*Pi<DC>/m));
for (int k = 0; k < n;k=k+m)
{
omega =complex<DC>(1,0);
int halfm = m >> 1;
for (int j = 0; j < halfm;++j)
{
complex<DC> t=omega*A[k+j+halfm];
complex<DC> u = A[k + j];
A[k+j]=u+t;
A[k + j + halfm] = u - t;
omega = omega * omega_m;
}
}
}
return A;
}
template<class DC> vector<complex<DC>> recursive_FFT(const vector<complex<DC>>& a,bool _jud)
{
int n = a.size();
if(n==1)
return a;
int halfn = (n >> 1);
complex<DC> omega_n;
if(_jud)
omega_n=complex<DC>(cos(2 * Pi<DC>/n),sin(2*Pi<DC>/n));
else
omega_n=complex<DC>(cos(2 * Pi<DC>/n),sin(0-2*Pi<DC>/n));
complex<DC> omega{1, 0};
vector<complex<DC>> a_even(halfn);
vector<complex<DC>> a_odd(halfn);
for (int i = 0;i<halfn;++i)
{
int twicei = i << 1;
a_even[i] = a[twicei];
a_odd[i] = a[twicei+1];
}
vector<complex<DC>> y_even= recursive_FFT(a_even,_jud);
vector<complex<DC>> y_odd = recursive_FFT(a_odd,_jud);
vector<complex<DC>> y(n);
for (int k = 0; k < halfn;++k)
{
complex<DC> temp = omega * y_odd[k];
y[k] = y_even[k] + temp;
y[k + halfn] = y_even[k] - temp;
omega = omega * omega_n;
}
return y;
}
template<class DC> vector<complex<DC>> DFT(const vector<complex<DC>> &a)
{
return iterative_FFT(a, true);
}
template<class DC> vector<DC> Product(const vector<DC>& a,const vector<DC> &b)
{
int n = a.size()<b.size()?a.size():b.size();
vector<DC> c(n);
for (int i = 0; i < n;++i)
c[i] = a[i] * b[i];
return c;
}
template<class DC> vector<complex<DC>> IDFT(const vector<complex<DC>>& a)
{
unsigned n = a.size();
vector<complex<DC>> r = iterative_FFT(a, false);
for (unsigned i=0;i<n;++i)
r[i] = r[i] /DC(n);
return r;
}
template<class DC> vector<complex<DC>> FFT(const vector<complex<DC>> &x,const vector<complex<DC>> &y)
{
int n = x.size();
vector<complex<DC>> a=x;
vector<complex<DC>> b=y;
a.resize(n << 1);
b.resize(n << 1);
auto r=IDFT(Product(DFT(a), DFT(b)));
r.pop_back();
return r;
}
template<class DC> vector<DC> RealFFT(const vector<DC> &x,const vector<DC> &y)
{
int n = x.size();
vector<complex<DC>> a(n);
vector<complex<DC>> b(n);
for (int i = 0; i < n;++i)
{
a[i] = complex<DC>(x[i], 0);
b[i] = complex<DC>(y[i], 0);
}
auto c = FFT(a, b);
int m = c.size();
vector<DC> r(m);
for (int i = 0;i<m;++i)
r[i]=c[i].real();
return r;
}
template<class DC> vector<vector<DC>> _AddZero(const vector<DC> &x,const vector<DC> &y)
{
uint32_t n = x.size() > y.size() ? x.size() : y.size();
uint32_t nearest = 1 << uint8_t(log2(n));
if(n!=nearest)
nearest = nearest << 1;
vector<DC> a=x;
vector<DC> b=y;
a.resize(nearest);
b.resize(nearest);
return {a, b};
}
template<class DC> vector<complex<DC>> Convolution(const vector<complex<DC>>&x,const vector<complex<DC>>& y)
{
unsigned n = x.size() > y.size() ? x.size() : y.size();
unsigned nearest = 1 << unsigned(log2(n));
if(n!=nearest)
nearest = nearest << 1;
vector<complex<DC>> a=x;
vector<complex<DC>> b=y;
a.resize(nearest);
b.resize(nearest);
auto r=FFT(a, b);
r.resize(x.size() + y.size() - 1);
return r;
}
template<class DC> vector<DC> RealConvolution(const vector<DC>&x,const vector<DC>& y)
{
vector<vector<DC>> ab = _AddZero(x, y);
auto r=RealFFT(ab[0], ab[1]);
r.resize(x.size() + y.size() - 1);
return r;
}
template<class DC> DC ModPower(DC a,DC n,DC mod)
{
DC m=a;
DC b=1;
while(n>=1)
{
if(n&1)
{
b =DC(1)*m*b%mod;
}
n>>=1;
m=DC(1)*m*m%mod;
}
return b;
}
template<class DC> vector<DC> ModProduct(const vector<DC>& a,const vector<DC> &b,DC mod)
{
uint32_t n = a.size()<b.size()?a.size():b.size();
vector<DC> c(n);
for (uint32_t i = 0; i < n;++i)
c[i] = DC(1)*a[i]*b[i]%mod;
return c;
}
template<class DC> vector<DC> iterative_NTT(const vector<DC>&a,bool _jud)
{
const DC g = 3; /* g is the minimal primitive root of mod */
const DC gi = 332748118; /* gi is the modular inverse of g modulo mod */ /* 332748118 */ /* 1398101334 */
const DC mod = 998244353; /* mod must be a prime */ /* 998244353 */ /* 4194304001 */
vector<DC> A= bit_reverse_copy(a);
uint32_t n = a.size();
DC omega, omega_m;
uint32_t halfm;
for (uint32_t m=2; m<=n; m<<=1)
{
if(_jud)
omega_m=ModPower(g,(mod-1)/DC(m),mod);
else
omega_m=ModPower(gi,(mod-1)/DC(m),mod);
for (uint32_t k = 0; k < n;k=k+m)
{
omega =1;
halfm = m >> 1;
for (uint32_t j = 0; j < halfm;++j)
{
DC t=DC(1)*omega*A[k+j+halfm]%mod;
DC u = A[k + j];
A[k+j]=(u+t)%mod;
A[k + j + halfm] =(mod+u - t)%mod;
omega =DC(1)*omega * omega_m%mod;
}
}
}
if(!_jud)
{
DC n_inverse=ModPower(DC(n),mod-2,mod);
for (uint32_t i = 0; i < n;++i)
A[i]=DC(1)*A[i]*n_inverse%mod;//这里要乘长度的逆元
}
return A;
}
template<class DC> vector<DC> NTT(const vector<DC> &x, const vector<DC> &y)
{
uint32_t n=x.size();
vector<DC> a = x;
vector<DC> b = y;
a.resize(n << 1);
b.resize(n << 1);
const DC mod=998244353; /* previous mod = 998244353 */ /* 4194304001 */
vector<DC> r=iterative_NTT(ModProduct(iterative_NTT(a, true), iterative_NTT(b, true),mod), false);
r.pop_back();
return r;
}
template<class DC> vector<DC> IntConvolution(const vector<DC> &x,const vector<DC> &y)
{
vector<vector<DC>> ab = _AddZero(x, y);
vector<DC> r=NTT(ab[0], ab[1]);
r.resize(x.size()+y.size()-1);
return r;
}
template<class DC> vector<DC> CarryBit(const vector<DC>& a,uint32_t bit)
{
vector<DC> r=a;
uint32_t n = r.size();
for (uint32_t i = 0; i <n-1;++i)
{
if(r[i]>=bit)
{
r[i + 1] += r[i] / bit;
r[i] = r[i] % bit;
}
}
while(r[r.size()-1]>=bit)
{
r.emplace_back(r[r.size()-1]/bit);
r[r.size() - 2] = r[r.size()-2] % bit;
}
return r;
}
template<class DC> string BitToString(const vector<DC> &a)
{
//auto start = std::chrono::system_clock::now();
/*uint32_t n = a.size();
string str,temp;
for (uint32_t i = 0; i < a.size();++i)
{
temp = std::to_string(a[i]);
reverse(temp.begin(), temp.end());
if(temp.size()<d)
temp.append(d - temp.size(), '0');
str.append(temp);
}*/
string str(a.size(),' ');
std::transform(a.begin(), a.end(), str.begin(), [](DC c)
{ return '0'+c; });
return str;
}
//杂例I
template<class DM> double Norm(const vector<DM> &a, double p)
{
if(p==INFINITY)
{
double max = 0;
for (long long unsigned int i = 0; i < a.size();++i)
{
if(Abs(a[i])>max)
max = Abs(a[i]);
}
return max;
}
double s = 0;
for (long long unsigned int i = 0; i < a.size();++i)
{
s += pow(Abs(a[i]), p);
}
s = pow(s, 1 / p);
return s;
}
template<class DJ> DJ Abs(DJ a)
{
if(a>0)
return a;
return -a;
}
template<class DJ> DJ Sign(DJ a)
{
if(a>0)
return 1;
if(a<0)
return -1;
return 0;
}
template <class DJ> vector<DJ> Range(DJ a, DJ b,DJ c)
{
vector<DJ> r;
DJ s = a;
while(s<=b)
{
r.emplace_back(s);
s = s + DJ(c);
}
return r;
}
template <class DJ> vector<DJ> Range(DJ a, DJ b)
{
return Range(a, b, DJ(1));
}
template <class DJ> vector<DJ> Rand(int a)
{
vector<DJ> r(a);
for (int i = 0;i<a;++i)
r[i]=std::rand()*Sig();
return r;
}
template <class DJ> DJ Limit(std::function<DJ(DJ)> f, DJ x0)
{
const DJ h = 1e-7;
return 0.5 * (f(x0+h)+f(x0-h));
}
template <class DJ> DJ N(DJ a,int n)
{
cout << std::setprecision(n) << a << '\n';
return a;
}
//多项式类
const Polynomial<double> X({0, 1}, 1);
template<class DC> Polynomial<DC>::Polynomial()
{
poly_deg = 0;
poly_coeff.push_back(0);
}
template<class DC> Polynomial<DC>::Polynomial(const vector<DC> &coeff)
{
poly_deg = coeff.size() - 1;
poly_coeff = coeff;
}
template<class DC> Polynomial<DC>::Polynomial(const vector<DC> &s,int deg)
{
poly_deg = deg;
poly_coeff = s;
}
template<class DC> Polynomial<DC>::Polynomial(DC r)
{
poly_deg = 0;
poly_coeff.push_back(r);
}
template<class DC> Polynomial<DC> &Polynomial<DC>::operator=(const Polynomial<DC> &f)
{
poly_deg = f.poly_deg;
poly_coeff=f.poly_coeff;
return *this;
}
template<class DC> DC Polynomial<DC>::operator()(DC t)
{
DC s=poly_coeff[poly_deg];
for (int i = poly_deg-1; i >= 0;--i)
s = s * t + poly_coeff[i];
return s;
}
template<class DC> Polynomial<DC> operator+(const Polynomial<DC> &a,const Polynomial<DC> &b)
{
bool here = true;
int degmax = a.poly_deg;
if(b.poly_deg>a.poly_deg)
{
degmax=b.poly_deg;
here = false;
}
vector<DC> r(degmax + 1);
Polynomial<DC> c;
c.poly_deg = degmax;
c.poly_coeff = r;
if(here)
{
for (int i = 0; i <=b.poly_deg;++i)
{
c.poly_coeff[i] = a.poly_coeff[i] + b.poly_coeff[i];
}
for (int i = b.poly_deg + 1;i<=c.poly_deg;++i)
{
c.poly_coeff[i] = a.poly_coeff[i];
}
return c;
}
for (int i = 0; i <=a.poly_deg;++i)
{
c.poly_coeff[i] = a.poly_coeff[i] + b.poly_coeff[i];
}
for (int i = a.poly_deg + 1;i<=c.poly_deg;++i)
{
c.poly_coeff[i] = b.poly_coeff[i];
}
return c;
}
template<class DC> Polynomial<DC> operator-(const Polynomial<DC> &a,const Polynomial<DC> &b)
{
bool here = true;
int degmax = a.poly_deg;
if(b.poly_deg>a.poly_deg)
{
degmax=b.poly_deg;
here = false;
}
vector<DC> r(degmax + 1);
Polynomial<DC> c;
c.poly_deg = degmax;
c.poly_coeff = r;
if(here)
{
for (int i = 0; i <=b.poly_deg;++i)
{
c.poly_coeff[i] = a.poly_coeff[i] - b.poly_coeff[i];
}
for (int i = b.poly_deg + 1;i<=c.poly_deg;++i)
{
c.poly_coeff[i] = a.poly_coeff[i];
}
return c;
}
for (int i = 0; i <=a.poly_deg;++i)
{
c.poly_coeff[i] = a.poly_coeff[i] - b.poly_coeff[i];
}
for (int i = a.poly_deg + 1;i<=c.poly_deg;++i)
{
c.poly_coeff[i] = DC(0)-b.poly_coeff[i];
}
return c;
}
template <class DD> Polynomial<DD> operator+(DD a,const Polynomial<DD> &f)
{
Polynomial<DD> p = f;
p.poly_coeff[0] += a;
return p;
}
template <class DD> Polynomial<DD> operator*(DD a,const Polynomial<DD> &f)
{
vector<DD> r;
for (int i = 0; i <= f.poly_deg;++i)
{
r.push_back(a * f.poly_coeff[i]);
}
Polynomial<DD> p;
p.poly_deg = f.poly_deg;
p.poly_coeff = r;
return p;
}
template <class DD> Polynomial<DD> operator*(const Polynomial<DD> &a, const Polynomial<DD> &b)
{
int n = a.poly_deg > b.poly_deg ? a.poly_deg : b.poly_deg;
if (a.poly_deg == 0)
return a.poly_coeff[0] * b;
if(b.poly_deg==0)
return b.poly_coeff[0] * a;
if(n==1)
{
vector<DD> r(3);
r[0]=a.poly_coeff[0]*b.poly_coeff[0];
r[2]=a.poly_coeff[1]*b.poly_coeff[1];
r[1] = (a.poly_coeff[0] + a.poly_coeff[1]) * (b.poly_coeff[0] + b.poly_coeff[1]) - r[0] - r[2];
Polynomial<DD> f;
f.poly_deg=2;
f.poly_coeff = r;
return f;
}
Polynomial<DD> p=a;
Polynomial<DD> q=b;
if(a.poly_deg>b.poly_deg)
{
q.poly_coeff.insert(q.poly_coeff.end(), a.poly_deg - b.poly_deg, 0);
q.poly_deg = n;
}
if(b.poly_deg>a.poly_deg)
{
p.poly_coeff.insert(p.poly_coeff.end(), b.poly_deg - a.poly_deg, 0);
p.poly_deg = n;
}
Polynomial<DD> p0 = SubPoly(p, 0, n / 2);
Polynomial<DD> p1 =SubPoly(p, n/2+1, n);
Polynomial<DD> q0=SubPoly(q,0,n/2);
Polynomial<DD> q1 = SubPoly(q, n/2+1, n);
Polynomial<DD> r0 = p0 * q0;
Polynomial<DD> r1=p1*q1;
Polynomial<DD> r2=(p0+p1)*(q0+q1);
return r0 + ((r2 - r0 - r1) >> (n/2+1)) + (r1 >> ((n/2+1)<<1));
}
template <class DD> Polynomial<DD> operator/(const Polynomial<DD>& f,const Polynomial<DD>& g)
{
cerr<<"尚待开发"<<'\n';
return f;
}
template<class DC> Polynomial<DC> operator^(const Polynomial<DC> &a,int n)
{
if(n<0)
{
cerr << "错误:多项式不能有负的幂次。" << '\n';
return a;
}
Polynomial<DC> m=a;
Polynomial<DC> b({1},0);
while(n>=1)
{
if(n&1)
{
b = m * b;
}
n=n>>1;
m = m * m;
}
return b;
}
template<class DC> ostream & operator<<(ostream & os, const Polynomial<DC> &f)
{
os << f.poly_coeff[0];
for (int i =1;i<=f.poly_deg;++i)
{
if(f.poly_coeff[i]==0)
continue;
if(f.poly_coeff[i]>0)
{
os << '+';
}
os << f.poly_coeff[i] << "x" ;
if(i!=1)
{
os << '^'<<i;
}
}
return os;
}
template<class DC> Polynomial<DC> &Polynomial<DC>::operator+=(const Polynomial<DC> &b)
{
*this = (*this) + b;
return *this;
}
template<class DC> Polynomial<DC> &Polynomial<DC>::operator-=(const Polynomial<DC> &b)
{
*this = (*this)- b;
return *this;
}
template <class DD> Polynomial<DD> operator>>(const Polynomial<DD> &f, int a)
{
vector<DD> r(f.poly_deg+1+a);
for (int i = 0; i <= f.poly_deg;++i)
{
r[i + a] = f.poly_coeff[i];
}
Polynomial<DD> p;
p.poly_deg = f.poly_deg + a;
p.poly_coeff = r;
return p;
}
template <class DD> DD Get(const Polynomial<DD> &f, DD t)
{
DD s=f.poly_coeff[f.poly_deg];
for (int i = f.poly_deg-1; i >= 0;--i)
s = s * t + f.poly_coeff[i];
return s;
}
template <class DD> vector<DD> GetCoef(const Polynomial<DD> &f)
{
return f.poly_coeff;
}
template<class DC> void Disp(const Polynomial<DC> &f)
{
cout << f<<'\n'<<'\n';
}
template<class DC> int Deg(const Polynomial<DC> &f)
{
return f.poly_deg;
}
template <class DC> Polynomial<DC> SubPoly(const Polynomial<DC> &f, int a, int b)
{
if(a<0)
a = 0;
if(b>f.poly_deg)
b = f.poly_deg;
vector<DC> r;
for (int i = a; i <= b;++i)
{
r.push_back(f.poly_coeff[i]);
}
Polynomial<DC> p;
p.poly_deg=b-a;
p.poly_coeff = r;
return p;
}
template <class DC> Polynomial<DC> D(const Polynomial<DC> &f)
{
if(f.poly_deg==0)
{
Polynomial<DC> d({0},0);
return d;
}
int n=f.poly_deg;
vector<DC> r;
for (int i = 1; i <= n;++i)
{
r.push_back(i * f.poly_coeff[i]);
}
Polynomial<DC> d;
d.poly_deg = n - 1;
d.poly_coeff = r;
return d;
}
template <class DD> Polynomial<DD> Integrate(const Polynomial<DD> &f)
{
if(f.poly_deg==0 && f.poly_coeff[0]==0)
{
return Polynomial<DD>({0}, 1);
}
int deg = f.poly_deg;
vector<DD> r(deg+2);
for (int i = 1; i <= deg + 1;++i)
{
r[i] = f.poly_coeff[i - 1] / i;
}
return Polynomial<DD>(r, deg + 1);
}
template <class DD> DD Integrate(const Polynomial<DD> &f,DD a,DD b)
{
Polynomial<DD> g=Integrate(f);
return g(b) - g(a);
}