bzoj2194 快速傅立叶之二

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 400050
const double Pi = acos(-1.0);
inline int rd()
{
int f=1,c=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();}
return f*c;
}
struct cp
{
double x,y;
cp(){}
cp(double x,double y):x(x),y(y){}
};
cp operator + (cp &a,cp &b)
{
return cp(a.x+b.x,a.y+b.y);
}
cp operator - (cp &a,cp &b)
{
return cp(a.x-b.x,a.y-b.y);
}
cp operator * (cp &a,cp &b)
{
return cp(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
}
int to[N];
void fft(cp *a,int len,int k)
{
for(int i=0;i<len;i++)
if(i<to[i])swap(a[i],a[to[i]]);
for(int i=1;i<len;i<<=1)
{
cp w0(cos(Pi/i),k*sin(Pi/i));
for(int j=0;j<len;j+=(i<<1))
{
cp w(1,0);
for(int o=0;o<i;o++,w=w*w0)
{
cp w1 = a[j+o],w2 = a[j+o+i]*w;
a[j+o] = w1+w2;
a[j+o+i] = w1-w2;
}
}
}
}
int n,lim=1,l,ans[N];
cp a[N],b[N],c[N];
int main()
{
n = rd();
for(int i=0;i<n;i++)
a[n-i-1].x = rd(),b[i].x = rd();
while(lim<2*n)lim<<=1,l++;
for(int i=1;i<lim;i++)to[i] = ((to[i>>1]>>1)|((i&1)<<(l-1)));
fft(a,lim,1),fft(b,lim,1);
for(int i=0;i<lim;i++)c[i] = a[i]*b[i];
fft(c,lim,-1);
for(int i=0;i<n;i++)ans[n-i-1]=(int)(c[i].x/lim+0.5);
for(int i=0;i<n;i++)printf("%d\n",ans[i]);
return 0;
}

posted @ 2019-01-12 09:42  LiGuanlin  阅读(120)  评论(0编辑  收藏  举报