## BZOJ2179:FFT快速傅立叶

https://www.lydsy.com/JudgeOnline/problem.php?id=2179

FFT模板题

 1 #include<cmath>
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 const int maxn=140005;
6 const double pi=acos(-1);
7 int n,m=1,lg;
8 char s1[maxn],s2[maxn];
9 int rev[maxn],ans[maxn];
10 struct complex{
11     double x,y;
12     complex(){};
13     complex(double xx,double yy){x=xx,y=yy;}
14     complex operator+(const complex &a)const{return complex(a.x+x,a.y+y);}
15     complex operator-(const complex &a)const{return complex(x-a.x,y-a.y);}
16     complex operator*(const complex &a)const{return complex(x*a.x-y*a.y,x*a.y+y*a.x);}
17 }a[maxn],b[maxn],c[maxn];
18 void FFT(complex *a,int len,int sign){
19     for(int i=0;i<len;i++)if(rev[i]<i)swap(a[i],a[rev[i]]);
20     for(int i=2;i<=len;i<<=1){
21         complex wn(cos(2*pi/i*sign),sin(2*pi/i*sign));
22         for(int j=0;j<len;j+=i){
23             complex w(1,0);
24             for(int k=0;k<(i>>1);k++,w=w*wn){
25                 complex x=a[j+k],y=w*a[j+(i>>1)+k];
26                 a[j+k]=x+y,a[j+k+(i>>1)]=x-y;
27             }
28         }
29     }
30     if(sign==-1)for(int i=0;i<len;i++)a[i].x/=len;
31 }
32 int main(){
33     scanf("%d",&n);
34     scanf("%s%s",s1,s2);
35     for(int i=n-1;~i;i--)a[i].x=s1[n-1-i]-'0';
36     for(int i=n-1;~i;i--)b[i].x=s2[n-1-i]-'0';
37     n=n*2;
38     for(;m<=n;lg++,m<<=1);
39     for(int i=0;i<m;i++)rev[i]=(rev[i>>1]>>1)|((i&1)<<(lg-1));
40     FFT(a,m,1),FFT(b,m,1);
41     for(int i=0;i<m;i++)c[i]=a[i]*b[i];
42     FFT(c,m,-1);
43     for(int i=0;i<m;i++)ans[i]=(int)(c[i].x+0.5);
44     for(int i=0;i<m;i++)ans[i+1]+=ans[i]/10,ans[i]%=10;
45     if(ans[m])m++;
46     while(!ans[m]&&m)m--;
47     for(int i=m;~i;i--)printf("%d",ans[i]);
48     return 0;
49 }
View Code

posted on 2018-08-08 16:08  HYSBZ_mzf  阅读(102)  评论(0编辑  收藏  举报