bzoj3527【ZJOI2014】力

3908.jpg




令a[i]=q[i],b[i]=1/(i^2),则E[j]=∑(1≤j≤i-1)a[j]*b[i-j]-E[j]=∑(i+1≤j≤n)a[j]*b[j-i]。

左边就是卷积形式,直接FFT;右边和bzoj2194等价,翻转一个数组后FFT




#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define maxn 500005
using namespace std;
int nn,n,m,len,rev[maxn];
double q[maxn],ans[maxn];
const double pi=acos(-1.0);
struct cp
{
	double x,y;
	cp(double xx=0,double yy=0){x=xx;y=yy;}
	inline cp operator +(cp b){return (cp){x+b.x,y+b.y};}
	inline cp operator -(cp b){return (cp){x-b.x,y-b.y};}
	inline cp operator *(cp b){return (cp){x*b.x-y*b.y,x*b.y+y*b.x};}
}a[maxn],b[maxn],c[maxn];
inline void fft(cp *x,int n,int flag)
{
	F(i,0,n-1) if (rev[i]>i) swap(x[i],x[rev[i]]);
	for(int m=2;m<=n;m<<=1)
	{
		cp wn=cp(cos(2.0*pi/m*flag),sin(2.0*pi/m*flag));
		for(int i=0;i<n;i+=m)
		{
			cp w=cp(1.0);
			int mid=m>>1;
			F(j,0,mid-1)
			{
				cp u=x[i+j],v=x[i+j+mid]*w;
				x[i+j]=u+v;x[i+j+mid]=u-v;
				w=w*wn;
			}
		}
	}
	if (flag==-1) F(i,0,n-1) x[i].x/=n;
}
int main()
{
	scanf("%d",&n);nn=n;
	F(i,0,n-1) scanf("%lf",&q[i]);
	n=n*2-1;m=1;
	while (m<n) m<<=1,len++;
	n=m;
	F(i,0,n-1)
	{
		int x=i,y=0;
		F(j,1,len) (y<<=1)|=(x&1),x>>=1;
		rev[i]=y;
	}
	F(i,0,nn-1) a[i]=cp(q[i],0);
	F(i,nn,n-1) a[i]=cp();
	b[0]=cp();
	F(i,1,nn-1) b[i]=cp(1.0/((double)i*(double)i));
	F(i,nn,n-1) b[i]=cp();
	fft(a,n,1);fft(b,n,1);
	F(i,0,n-1) c[i]=a[i]*b[i];
	fft(c,n,-1);
	F(i,0,nn-1) ans[i]=c[i].x;
	F(i,0,nn-1) a[i]=cp(q[nn-1-i],0);
	F(i,nn,n-1) a[i]=cp();
	b[0]=cp();
	F(i,1,nn-1) b[i]=cp(1.0/((double)i*(double)i));
	F(i,nn,n-1) b[i]=cp();
	fft(a,n,1);fft(b,n,1);
	F(i,0,n-1) c[i]=a[i]*b[i];
	fft(c,n,-1);
	F(i,0,nn-1) ans[i]-=c[nn-1-i].x;
	F(i,0,nn-1) printf("%.5lf\n",ans[i]);
	return 0;
}


你可能感兴趣的:(fft,bzoj)