1. 程式人生 > >洛谷 P1919 【模板】A*B Problem升級版(FFT快速傅立葉)

洛谷 P1919 【模板】A*B Problem升級版(FFT快速傅立葉)

題目來源

吐槽下P3803都是紫題...

真心好寫,本想一遍過的...但是

我真是太菜了...

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=200000;
 4 const double pi=acos(-1.0);
 5 char x[MAXN],y[MAXN];
 6 int sum,lena,lenb,l;
 7 int TT[MAXN],c[MAXN];
 8 int r;
 9 int n,m;
10 struct Node{
11     //int x;
12
double x; 13 double y; 14 Node (double x1=0,double y1=0){ 15 x=x1,y=y1; 16 } 17 }a[MAXN],b[MAXN]; 18 Node operator * (Node x,Node y){ 19 return Node(x.x*y.x-x.y*y.y,x.x*y.y+x.y*y.x); 20 } 21 Node operator + (Node x,Node y){ 22 return Node(x.x+y.x,x.y+y.y); 23
} 24 Node operator - (Node x,Node y){ 25 return Node(x.x-y.x,x.y-y.y); 26 } 27 inline void fft(Node *g,double tf){ 28 for (int i=0;i<sum;++i) 29 if (i<c[i]) 30 swap(g[i],g[c[i]]); 31 for (int j=1;j<sum;j<<=1){ 32 Node T(cos(pi/j),tf*sin(pi/j));
33 for (int k=0;k<sum;k+=(j<<1)){ 34 Node T1(1,0); 35 for (int l=0;l<j;l++,T1=T*T1){ 36 Node x1=g[k+l]; 37 Node y1=T1*g[k+j+l]; 38 g[k+l]=x1+y1; 39 g[k+j+l]=x1-y1; 40 } 41 } 42 } 43 } 44 int main(){ 45 scanf("%d",&n); 46 scanf("%s%s",x,y); 47 //printf("%s",x); 48 for (int i=n-1;i>=0;i--){ 49 a[lena++].x=x[i]-48; 50 b[lenb++].x=y[i]-48; 51 } 52 sum=1; 53 while (sum<n+n) sum<<=1,l++; 54 for (int i=0;i<=sum;++i) 55 c[i]=(c[i>>1]>>1)|((i&1)<<(l-1)); 56 fft(a,1),fft(b,1); 57 for (int i=0;i<=sum;++i) 58 a[i]=a[i]*b[i]; 59 fft(a,-1); 60 for (int i=0;i<=sum;++i){ 61 TT[i]+=(int) (a[i].x/sum+0.5); 62 if (TT[i]>=10){ 63 TT[i+1]+=TT[i]/10; 64 TT[i]%=10; 65 if (i==sum) 66 sum++; 67 } 68 } 69 while (!TT[sum] && sum>=1) 70 sum--; 71 sum++; 72 while (--sum>=0) 73 printf("%d",TT[sum]); 74 return 0; 75 }