FFT模板
代码(p3803)
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cctype> #include<cmath> #include<cstdlib> #include<queue> #include<ctime> #include<vector> #include<set> #include<map> #include<stack> using namespace std; #define pi acos(-1.0) #define ri register int int n,m,r[2100000],len; struct node { double x,y; node(){}; node(double a,double b){ x=a,y=b; } }a[2100000],b[2100000]; inline int read(){ int x=0,f=1;char s=getchar(); while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();} while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s-'0');s=getchar();} return x*f; } inline node operator + (const node a,const node b){ return node(a.x+b.x,a.y+b.y); } inline node operator - (const node a,const node b){ return node(a.x-b.x,a.y-b.y); } inline node operator * (const node a,const node b){ return node(a.x*b.x-a.y*b.y,a.y*b.x+a.x*b.y); } inline void fft(node *a,int f){ for(ri i=0;i<n;i++) if(i<r[i]) swap(a[i],a[r[i]]); for(ri k=1;k<n;k<<=1){ node wn(cos(pi/k),f*sin(pi/k)); for(ri i=0;i<n;i+=(k<<1)){ node w(1,0),p,q; for(ri j=0;j<k;j++,w=w*wn){ p=a[i+j],q=a[i+j+k]*w; a[i+j]=p+q,a[i+j+k]=p-q; } } } if(f==-1) for(ri i=0;i<n;i++) a[i].x=a[i].x/n; } int main() { n=read(),m=read(); for(ri i=0;i<=n;i++)a[i].x=read(); for(ri i=0;i<=m;i++)b[i].x=read(); m+=n; for(n=1;n<=m;n<<=1)len++; for(ri i=0;i<n;i++)r[i]=((r[i>>1]>>1)|(i&1)<<(len-1)); fft(a,1),fft(b,1); for(ri i=0;i<n;i++)a[i]=a[i]*b[i]; fft(a,-1); for(ri i=0;i<=m;i++)printf("%d ",(int)(a[i].x+0.5)); return 0; }