FFT板子

woc......FFT这玩意儿真坑......

一上午除了打了几遍板子什么也没干......真是废了......

你要加油啊......

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=270010;
 7 const double pi=acos(-1.0),eps=1e-4;
 8 struct Complex{
 9     double a,b;
10     Complex(double a=0.0,double b=0.0):a(a),b(b){}
11     Complex operator+(const Complex &x)const{return Complex(a+x.a,b+x.b);}
12     Complex operator-(const Complex &x)const{return Complex(a-x.a,b-x.b);}
13     Complex operator*(const Complex &x)const{return Complex(a*x.a-b*x.b,a*x.b+b*x.a);}
14 }A[maxn],B[maxn];
15 void FFT(Complex*,int,int);
16 int n,m,N=1;
17 int main(){
18     scanf("%d%d",&n,&m);
19     n++;m++;
20     while(N<n+m)N<<=1;
21     for(int i=0;i<n;i++)scanf("%lf",&A[i].a);
22     for(int i=0;i<m;i++)scanf("%lf",&B[i].a);
23     FFT(A,N,1);
24     FFT(B,N,1);
25     for(int i=0;i<N;i++)A[i]=A[i]*B[i];
26     FFT(A,N,-1);
27     for(int i=0;i<n+m-1;i++)printf("%d ",(int)(A[i].a+eps));
28     return 0;
29 }
30 void FFT(Complex *A,int n,int tp){
31     for(int i=1,j=0;i<n-1;i++){
32         int k=N;
33         do{
34             k>>=1;
35             j^=k;
36         }while(j<k);
37         if(i<j)swap(A[i],A[j]);
38     }
39     for(int k=2;k<=n;k<<=1){
40         Complex wn(cos(-tp*2*pi/k),sin(-tp*2*pi/k));
41         for(int i=0;i<n;i+=k){
42             Complex w(1.0,0.0);
43             for(int j=0;j<(k>>1);j++,w=w*wn){
44                 Complex a(A[i+j]),b(w*A[i+j+(k>>1)]);
45                 A[i+j]=a+b;
46                 A[i+j+(k>>1)]=a-b;
47             }
48         }
49     }
50     if(tp<0)for(int i=0;i<n;i++)A[i].a/=n;
51 }
View Code

挖个大坑:

COGS2216 你猜是不是KMP

posted @ 2017-01-26 11:44  AntiLeaf  阅读(652)  评论(0编辑  收藏  举报