标签:
纪念人生第一次FFT
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 5 const int MAXN=200010; 6 class BigNum 7 { 8 public: 9 double r,i; 10 BigNum(double _r=0.0,double _i=0.0){r=_r;i=_i;} 11 BigNum operator+(const BigNum T){return BigNum(r+T.r,i+T.i);} 12 BigNum operator-(const BigNum T){return BigNum(r-T.r,i-T.i);}; 13 BigNum operator*(const BigNum T){return BigNum(r*T.r-i*T.i,r*T.i+i*T.r);}; 14 }; 15 16 void Brc(BigNum *T,int N) 17 { 18 int i,j,k; 19 for(i=1,j=N/2;i<N-1;i++) 20 { 21 if(i<j) swap(T[i],T[j]); 22 k=N/2; 23 while(j>=k) 24 { 25 j-=k; 26 k>>=1; 27 } 28 if(j<k) j+=k; 29 } 30 } 31 32 void FFT(BigNum *T,int N,int flag) 33 { 34 Brc(T,N); 35 for(int i=2;i<=N;i<<=1) 36 { 37 BigNum wn(cos(2*M_PI/i),sin(2*M_PI/i)); 38 for(int j=0;j<N;j+=i) 39 { 40 BigNum w(1,0); 41 for(int k=j;k<j+i/2;k++) 42 { 43 BigNum u=T[k]; 44 BigNum t=w*T[k+i/2]; 45 T[k]=u+t; 46 T[k+i/2]=u-t; 47 w=w*wn; 48 } 49 } 50 } 51 if(flag==-1) 52 for(int i=0;i<N;i++) 53 T[i].r/=N; 54 }
标签:
原文地址:http://www.cnblogs.com/InWILL/p/5937163.html