数学(FFT):BZOJ 3527 [Zjoi2014]力

时间:2024-11-17 19:34:25

题目在这里:http://wenku.baidu.com/link?url=X4j8NM14MMYo8Q7uPE7-7GjO2_TXnMFA2azEbBh4pDf7HCENM3-hPEl4mzoe2wSoblrSOvMirfS7PsQ1OVjsdaCJhEaGNCpuUxFKoPvNvXa

  裸的FFT,小心i*i爆int!!!

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct complex{
double r,i;
complex(long double r_=0.0,long double i_=0.0){
r=r_;i=i_;
}
complex operator+(complex a){
return complex(r+a.r,i+a.i);
}
complex operator-(complex a){
return complex(r-a.r,i-a.i);
}
complex operator*(complex a){
return complex(r*a.r-i*a.i,i*a.r+r*a.i);
}
}A[maxn],B[maxn],C[maxn]; void Rader(complex *a,int len){
for(int i=,j=len>>;i<len-;i++){
if(i<j)swap(a[i],a[j]);
int k=len>>;
while(j>=k){
j-=k;
k>>=;
}
j+=k;
}
} void FFT(complex *a,int len,int on){
Rader(a,len);
for(int h=;h<=len;h<<=){
complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j=;j<len;j+=h){
complex w(,);
for(int k=j;k<j+(h>>);k++){
complex x=a[k];
complex y=a[k+(h>>)]*w;
a[k]=x+y;
a[k+(h>>)]=x-y;
w=w*wn;
}
}
}
if(on==-)
for(int i=;i<len;i++)
a[i].r/=1.0*len;
} double f[maxn],E[maxn];
int main(){
int n,len=;
scanf("%d",&n);
while(len<=n*)len<<=;
for(int i=;i<=n;i++)
scanf("%lf",&f[i]); for(int i=;i<=n;i++){
A[i-].r=f[i];
B[n-i].r=f[i];
C[i].r=1.0/i/i;
}
FFT(A,len,);FFT(B,len,);FFT(C,len,);
for(int i=;i<len;i++)
A[i]=A[i]*C[i],B[i]=B[i]*C[i];
FFT(A,len,-);FFT(B,len,-);
for(int i=;i<n;i++){
E[i]=A[i].r-B[n-i-].r;
printf("%.3f\n",E[i]);
}
return ;
}