把一个n位数看做n-1次的多项式,每一项的系数是反过来的每一位
最后每一项系数进进位搞一搞就行了
(数组一定要开到2的次数..要不然极端数据会RE)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 const int maxn=132000; 7 const double Pi=acos(-1); 8 9 struct Cpx{ 10 double x,y; 11 Cpx(double xx=0,double yy=0){x=xx;y=yy;} 12 }X[maxn],Y[maxn]; 13 Cpx operator *(Cpx a,Cpx b){return Cpx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);} 14 Cpx operator +(Cpx a,Cpx b){return Cpx(a.x+b.x,a.y+b.y);} 15 Cpx operator -(Cpx a,Cpx b){return Cpx(a.x-b.x,a.y-b.y);} 16 int N,M,rev[maxn],ans[maxn]; 17 18 void rd(Cpx *A){ 19 char c=getchar(); 20 while(c<'0'||c>'9') c=getchar(); 21 int i=N-1; 22 while(c>='0'&&c<='9') A[i--].x=(int)(c-'0'),c=getchar(); 23 } 24 25 void fft(Cpx *A,int opt){ 26 for(int i=0;i<N;i++) if(i<rev[i]) swap(A[i],A[rev[i]]); 27 for(int l=1;l<N;l<<=1){ 28 Cpx wn=Cpx(cos(Pi/l),opt*sin(Pi/l));int step=l<<1; 29 for(int i=0;i<N;i+=step){ 30 Cpx w=Cpx(1,0); 31 for(int k=0;k<l;k++,w=w*wn){ 32 Cpx a=A[i+k],b=A[i+k+l]*w; 33 A[i+k]=a+b;A[i+k+l]=a-b; 34 } 35 } 36 } 37 } 38 39 int main(){ 40 int i,j,k; 41 scanf("%d",&N);M=N*2-1; 42 rd(X);rd(Y); 43 for(i=1,j=0;i<M;i<<=1,j++);N=i; 44 for(i=0;i<N;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(j-1)); 45 fft(X,1);fft(Y,1); 46 for(i=0;i<N;i++) X[i]=X[i]*Y[i];fft(X,-1); 47 for(i=0;i<M;i++) 48 j=(int)(X[i].x/N+0.5),ans[i+1]=(ans[i]+j)/10,ans[i]=(ans[i]+j)%10; 49 for(i=M;i>=0&&!ans[i];i--); 50 for(;i>=0;i--) printf("%d",ans[i]); 51 }