【bzoj2194】快速傅立叶之二

 我来水一发FFT模板,这题真没什么好说的。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<queue>
#include<algorithm>
using namespace std;
#define LL long long
#define FILE "dealing"
#define up(i,j,n) for(LL i=(j);i<=n;i++)
#define pii pair<LL,LL>
#define mem(i,j) memset(i,j,sizeof(i));
#define mid ((l+r)>>1)
#define db double
void cmax(LL& x,LL y){if(x<y)x=y;}
void cmin(LL& x,LL y){if(x>y)x=y;}
LL read(){
	LL x=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}
	return f*x;
}
const LL maxn=305000,inf=1000000000;
struct cp{
	db x,y;
	cp(db x=0,db y=0):x(x),y(y){}
	cp operator+(const cp& b){return cp(x+b.x,y+b.y);}
	cp operator-(const cp& b){return cp(x-b.x,y-b.y);}
	cp operator*(const cp& b){return cp(x*b.x-y*b.y,x*b.y+y*b.x);}
}a[maxn],b[maxn],w[maxn];
db pi=(acos(-1.0));
LL H=0,L=1,R[maxn],c[maxn],d[maxn];
void FFT(cp* a,LL f){
	for(LL i=0;i<L;i++)if(i<R[i])swap(a[i],a[R[i]]);
	for(LL len=2;len<=L;len<<=1){
		LL l=len>>1;
		cp wn(cos(pi/l),f*sin(pi/l));
		for(LL i=1;i<l;i++)w[i]=w[i-1]*wn;
		for(LL st=0;st<L;st+=len)
			for(LL k=0;k<l;k++){
				cp x=a[st+k],y=w[k]*a[st+k+l];
				a[st+k]=x+y,a[st+k+l]=x-y;
			}
	}
	if(f==-1)for(LL i=0;i<L;i++)a[i].x/=L;
}
LL n;
int main(){
	freopen("dealing.in","r",stdin);
	freopen("dealing.out","w",stdout);
	w[0].x=1;
	n=read();
	up(i,0,n-1)c[i]=read(),d[i]=read();
	up(i,0,n-1)a[i].x=c[i],b[i].x=d[n-i-1];
	for(H=0,L=1;L<n*2;H++)L<<=1;
	for(LL i=0;i<L;i++)R[i]=(R[i>>1]>>1)|((i&1)<<(H-1));
	FFT(a,1);FFT(b,1);
	for(LL i=0;i<L;i++)a[i]=a[i]*b[i];
	FFT(a,-1);
	for(LL i=0;i<L;i++)c[i]=(LL)(a[i].x+0.5);
	up(i,n-1,n*2-2)printf("%lld\n",c[i]);
	return 0;
}

  

posted @ 2017-03-04 22:48  CHADLZX  阅读(75)  评论(0编辑  收藏  举报