bzoj3527: [Zjoi2014]力

一句话:给出n个数qi,给出Fj的定义如下: 
 

令Ei=Fi/qi,求Ei


思路:先把q[i]约了,然后就是:


Ei=j<iqj(ji)2j>iqj(ji)2


先看左边:

令f[i]=q[i],g[i]=1/i/i

左边就是sigma f[j]*g[i-j] 

然后下标和就为定值了,就是卷积了,上FFT搞一搞

右边把q反过来再上FFT搞一搞


j<i

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const double pi=M_PI;
const int maxn=270010;
using namespace std;
struct plex{
	double r,i;
}tmp[maxn];
plex operator +(plex a,plex b){return (plex){a.r+b.r,a.i+b.i};}
plex operator -(plex a,plex b){return (plex){a.r-b.r,a.i-b.i};}
plex operator *(plex a,plex b){return (plex){a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r};}
int n,nn;double q[maxn],ans[maxn];

struct DFT{
	plex a[maxn];
	void fft(int bg,int step,int size,int op){
		if (size==1) return;
		fft(bg,step<<1,size>>1,op),fft(bg+step,step<<1,size>>1,op);
		plex w=(plex){1,0},t=(plex){cos(2*pi/size),sin(2.0*pi*op/size)};
		int p=bg,p0=bg,p1=bg+step;
		for (int i=0;i<size/2;i++){
			tmp[p]=a[p0]+w*a[p1];
			tmp[p+size/2*step]=a[p0]-w*a[p1];
			p+=step,p0+=step*2,p1+=step*2,w=w*t;
		}
		for (int i=bg;size;i+=step,size--) a[i]=tmp[i];
	}
}a,b,c;

int main(){
	scanf("%d",&n);
	for (nn=1;nn<(n<<1);nn<<=1);
	for (int i=0;i<n;i++) scanf("%lf",&q[i]);
	for (int i=1;i<n;i++) b.a[i]=(plex){1.0/i/i,0};
	for (int i=0;i<n;i++) a.a[i]=(plex){q[i],0};
	a.fft(0,1,nn,1),b.fft(0,1,nn,1);
	for (int i=0;i<nn;i++) c.a[i]=a.a[i]*b.a[i];
	c.fft(0,1,nn,-1);
	for (int i=0;i<n;i++) ans[i]+=c.a[i].r/nn;
	for (int i=0;i<n;i++) a.a[i]=(plex){q[n-i-1],0};
	for (int i=n;i<nn;i++) a.a[i]=(plex){0,0};
	a.fft(0,1,nn,1);
	for (int i=0;i<nn;i++) c.a[i]=a.a[i]*b.a[i];
	c.fft(0,1,nn,-1);
	for (int i=0;i<n;i++) ans[i]-=c.a[n-i-1].r/nn;
	for (int i=0;i<n;i++) printf("%.7f\n",ans[i]);
	return 0;
}



j<i

你可能感兴趣的:(fft)