bzoj3527 -- FFT
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 #define N 800010 8 #define PI acos(-1.0) 9 struct E{ 10 double r,i; 11 E(){} 12 E(double r,double i):r(r),i(i){} 13 void operator /= (double a){r/=a;i/=a;} 14 E operator + (E a){return E(r+a.r,i+a.i);} 15 E operator - (E a){return E(r-a.r,i-a.i);} 16 E operator * (E a){return E(r*a.r-i*a.i,r*a.i+i*a.r);} 17 }a[N],b[N],c[N]; 18 int i,j,k,n,m,M,r[N],l; 19 double p[N],q[N]; 20 inline void FFT(E* a,int n,int d){ 21 for(int i=0;i<n;i++)if(r[i]>i)swap(a[i],a[r[i]]); 22 for(int i=1;i<n;i<<=1){ 23 E wn(cos(PI/i),d*sin(PI/i)); 24 for(int j=0;j<n;j+=i<<1){ 25 E w(1,0); 26 for(int k=0;k<i;k++){ 27 E x=a[j+k],y=a[j+k+i]*w; 28 a[j+k]=x+y;a[j+k+i]=x-y; 29 w=w*wn; 30 } 31 } 32 } 33 if(d==-1)for(int i=0;i<n;i++)a[i]/=n; 34 } 35 int main() 36 { 37 scanf("%d",&n); 38 for(i=1;i<=n;i++)scanf("%lf",&q[i]),b[i+n].r=q[i]; 39 for(i=0;i<=n<<1;i++) 40 if(i<n)p[i]=-1.0/(1ll*(n-i)*(n-i));else 41 if(i>n)p[i]=1.0/(1ll*(n-i)*(n-i));else p[i]=0; 42 n<<=1; 43 for(i=0;i<=n;i++)a[i].r=p[i]; 44 for(M=1;M<=n<<1;M<<=1)l++; 45 for(i=0;i<M;i++)r[i]=(r[i>>1]>>1)|((i&1)<<l-1); 46 FFT(a,M,1);FFT(b,M,1); 47 for(i=0;i<M;i++)a[i]=a[i]*b[i]; 48 FFT(a,M,-1); 49 for(i=n+1;i<=n+(n>>1);i++)printf("%.3lf\n",a[i].r); 50 return 0; 51 }