很好的一篇博客
这是我的代码
#include<bits/stdc++.h>
using namespace std;
const int N=4000003;
const double pi=acos(-1.0);
#define C Complex
int r[N],n,m,i,lim=1,l;
struct C{
double x,y;
C(double xx=0,double yy=0){x=xx;y=yy;}
}a[N],b[N];
C operator +(C a,C b){return C(a.x+b.x,a.y+b.y);}
C operator -(C a,C b){return C(a.x-b.x,a.y-b.y);}
C operator *(C a,C b){return C(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
int read(){
char c;int x=0,f=1;
do{c=getchar();if (c=='-') f=-1;}while (c<'0' || c>'9');
while ('0'<=c && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return f*x;
}
void fft(Complex *A,int opt){
for (int i=0;i<lim;i++)
if (i<r[i]) swap(A[i],A[r[i]]);
for (int mid=1;mid<lim;mid<<=1){
C wn(cos(pi/mid),opt*sin(pi/mid));
for (int R=mid<<1,j=0;j<lim;j+=R){
C w(1,0);
for (int k=0;k<mid;k++,w=w*wn){
C x=A[j+k],y=w*A[j+k+mid];
A[j+k]=x+y;
A[j+k+mid]=x-y;
}
}
}
}
int main(){
n=read();m=read();
for (i=0;i<=n;i++) a[i].x=read();
for (i=0;i<=m;i++) b[i].x=read();
for (;lim<=n+m;lim<<=1,l++);
for (i=0;i<lim;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
fft(a,1);fft(b,1);
for (i=0;i<lim;i++) a[i]=a[i]*b[i];
fft(a,-1);
for (i=0;i<=n+m;i++) printf("%d ",(int)(a[i].x/lim+0.5));
}