BZOJ3527:[ZJOI2014]力(FFT)
Description
给出n个数qi,给出Fj的定义如下:
令Ei=Fi/qi,求Ei.
Input
第一行一个整数n。
接下来n行每行输入一个数,第i行表示qi。
n≤100000,0<qi<1000000000
Output
n行,第i行输出Ei。与标准答案误差不超过1e-2即可。
Sample Input
5
4006373.885184
15375036.435759
1717456.469144
8514941.004912
1410681.345880
4006373.885184
15375036.435759
1717456.469144
8514941.004912
1410681.345880
Sample Output
-16838672.693
3439.793
7509018.566
4595686.886
10903040.872
3439.793
7509018.566
4595686.886
10903040.872
Solution
不会markdown/LaTeX的下场就是只能像我一样在word里编辑好然后粘图
Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #define N (400000+100) 6 using namespace std; 7 8 double pi=acos(-1.0); 9 double q[N],g[N],ans[N]; 10 int n,fn,l,r[N]; 11 struct complex 12 { 13 double x,y; 14 complex(double xx=0,double yy=0) 15 { 16 x=xx; y=yy; 17 } 18 }a[N],b[N]; 19 20 complex operator + (complex a,complex b){return complex(a.x+b.x,a.y+b.y);} 21 complex operator - (complex a,complex b){return complex(a.x-b.x,a.y-b.y);} 22 complex operator * (complex a,complex b){return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);} 23 complex operator / (complex a,double b){return complex(a.x/b,a.y/b);} 24 25 void FFT(int n,complex *a,int opt) 26 { 27 for (int i=0; i<n; ++i) 28 if (i<r[i]) 29 swap(a[i],a[r[i]]); 30 for (int k=1; k<n; k<<=1) 31 { 32 complex wn=complex(cos(pi/k),opt*sin(pi/k)); 33 for (int i=0; i<n; i+=(k<<1)) 34 { 35 complex w=complex(1,0); 36 for (int j=0; j<k; ++j,w=w*wn) 37 { 38 complex x=a[i+j], y=w*a[i+j+k]; 39 a[i+j]=x+y; a[i+j+k]=x-y; 40 } 41 } 42 } 43 if (opt==-1) for (int i=0; i<n; ++i) a[i]=a[i]/n; 44 } 45 46 void Calc() 47 { 48 memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); 49 for (int i=0; i<=n; ++i) a[i].x=q[i]; 50 for (int i=0; i<=n; ++i) b[i].x=g[i]; 51 FFT(fn,a,1); FFT(fn,b,1); 52 for (int i=0; i<=fn; ++i) 53 a[i]=a[i]*b[i]; 54 FFT(fn,a,-1); 55 } 56 57 int main() 58 { 59 scanf("%d",&n); 60 for (int i=1; i<=n; ++i) 61 scanf("%lf",&q[i]); 62 for (int i=1; i<=n; ++i) 63 g[i]=1.0/i/i; 64 fn=1; 65 while (fn<=n+n) fn<<=1, l++; 66 for (int i=0; i<fn; ++i) 67 r[i]=(r[i>>1]>>1) | ((i&1)<<(l-1)); 68 69 Calc(); 70 for (int i=1; i<=n; ++i) 71 ans[i]=a[i].x; 72 for (int i=1; i<=n/2; ++i) 73 swap(q[i],q[n-i+1]); 74 Calc(); 75 for (int i=1; i<=n; ++i) 76 printf("%.6lf\n",ans[i]-a[n-i+1].x); 77 }