题目大意:求$F[i]=sumlimits_{j<i}frac{q_{j}}{(i-j)^2}-sumlimits_{j>i}frac{q_{j}}{(i-j)^2}$
写的第一道不是板子的$FFT$题..然而并没有想出来
原式其实是一个类似于卷积的形式,考虑转化
令$f[i]=frac{1}{i^2}$,则原式可以转化为$F[i]=sumlimits_{j<i}q[j]f[i-j]-sumlimits_{j>i}q[j]f[i-j]$
正着反着各跑一边$FFT$即可
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define N1 (1<<18)+10 6 #define M1 40010 7 #define ll long long 8 #define dd double 9 #define inf 0x3f3f3f3f 10 using namespace std; 11 12 int gint() 13 { 14 int ret=0,fh=1;char c=getchar(); 15 while(c<'0'||c>'9'){if(c=='-')fh=-1;c=getchar();} 16 while(c>='0'&&c<='9'){ret=ret*10+c-'0';c=getchar();} 17 return ret*fh; 18 } 19 20 const dd pi=acos(-1); 21 struct cp{ 22 dd x,y; 23 friend cp operator + (const cp &s1,const cp &s2){ return (cp){s1.x+s2.x,s1.y+s2.y}; } 24 friend cp operator - (const cp &s1,const cp &s2){ return (cp){s1.x-s2.x,s1.y-s2.y}; } 25 friend cp operator * (const cp &s1,const cp &s2){ return (cp){s1.x*s2.x-s1.y*s2.y,s1.x*s2.y+s1.y*s2.x}; } 26 }A[N1],B[N1],C[N1]; 27 28 void clr() 29 { 30 memset(A,0,sizeof(A)); 31 memset(B,0,sizeof(B)); 32 memset(C,0,sizeof(C)); 33 } 34 35 int n,m,r[N1]; 36 void FFT(cp *s,int len,int type) 37 { 38 int i,j,k; 39 for(i=0;i<len;i++) if(i<r[i]) swap(s[i],s[r[i]]); 40 for(k=2;k<=len;k<<=1) 41 { 42 cp wn=(cp){cos(2.0*pi*type/k),sin(2.0*pi*type/k)},t,w; 43 for(i=0;i<len;i+=k) 44 { 45 w=(cp){1,0}; 46 for(j=0;j<(k>>1);j++,w=w*wn) 47 { 48 t=w*s[i+j+(k>>1)]; 49 s[i+j+(k>>1)]=s[i+j]-t; 50 s[i+j]=s[i+j]+t; 51 } 52 } 53 } 54 } 55 void FFT_Main(int len) 56 { 57 FFT(A,len,1); FFT(B,len,1); 58 for(int i=0;i<len;i++) C[i]=A[i]*B[i]; 59 FFT(C,len,-1); 60 for(int i=0;i<len;i++) C[i].x/=len; 61 } 62 63 int len,L; 64 dd q[N1],F[N1],g[N1],ans[N1]; 65 66 int main() 67 { 68 scanf("%d",&n); int i; 69 for(i=0;i<n;i++) scanf("%lf",&q[i]); 70 for(len=1,L=0;len<n+n;len<<=1,L++); 71 for(i=0;i<len;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(L-1)); 72 for(i=1;i<n;i++) A[i].x=q[i],B[i].x=1.0/i/i; A[0].x=q[0]; 73 FFT_Main(len); 74 for(i=0;i<n;i++) ans[i]=C[i].x; 75 clr(); 76 for(i=1;i<n;i++) A[i].x=q[n-i-1],B[i].x=1.0/i/i; A[0].x=q[n-1]; 77 FFT_Main(len); 78 for(i=0;i<n;i++) ans[i]=(ans[i]-C[n-i-1].x); 79 for(i=0;i<n;i++) printf("%lf ",ans[i]); 80 return 0; 81 }