#include<bits/stdc++.h>
#define LL long long
#define V inline void
#define I inline int
#define FOR(i,a,b) for(register int i=a,end##i=b;i<=end##i;++i)
#define REP(i,a,b) for(register int i=a,end##i=b;i>=end##i;--i)
#define go(i,x) for(int i=hed[x];i;i=e[i].pre)
using namespace std;
inline int read()
{
char x='\0';
int fh=1,sum=0;
for(x=getchar();x<'0'||x>'9';x=getchar())if(x=='-')fh=-1;
for(;x>='0'&&x<='9';x=getchar())sum=sum*10+x-'0';
return fh*sum;
}
const int N=4e6+9;
const double pi=acos(-1);
struct plx{
double x,y;
plx(double xx=0,double yy=0){x=xx,y=yy;}
friend plx operator+(plx a,plx b){return plx(a.x+b.x,a.y+b.y);}
friend plx operator-(plx a,plx b){return plx(a.x-b.x,a.y-b.y);}
friend plx operator*(plx a,plx b){return plx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
}a[N],b[N];
int n,m,rev[N];
V getrev(int len){FOR(i,0,len-1)rev[i]=((rev[i>>1]>>1)|((i&1)*(len>>1)));}
V FFT(plx *A,int len,int tp)
{
FOR(i,0,len-1)if(i<rev[i])swap(A[i],A[rev[i]]);
for(int mid=1;mid<len;mid<<=1)
{
plx wn=plx(cos(pi/mid),sin(pi/mid));
for(int i=0;i<len;i+=mid*2)
{
plx w=plx(1,0);
for(int j=0;j<mid;j++,w=w*wn)
{
plx x=A[i+j],y=A[i+mid+j]*w;
A[i+j]=x+y,A[i+mid+j]=x-y;
}
}
}
if(tp==-1)
{
reverse(A+1,A+len);
FOR(i,0,len-1)A[i].x=(A[i].x>0)?(int)(A[i].x/len+0.5):(int)(A[i].x/len-0.5);
}
}
int main()
{
n=read()+1,m=read()+1;
FOR(i,0,n-1)a[i].x=read();
FOR(i,0,m-1)b[i].x=read();
int len=1;
while(len<n+m-1)len<<=1;
getrev(len);
FFT(a,len,1),FFT(b,len,1);
FOR(i,0,len-1)a[i]=a[i]*b[i];
FFT(a,len,-1);
FOR(i,0,n+m-2)printf("%d ",(int)a[i].x);
return 0;
}
预处理单位根:
#include<bits/stdc++.h>
#define LL long long
#define V inline void
#define I inline int
#define FOR(i,a,b) for(register int i=a,end##i=b;i<=end##i;++i)
#define REP(i,a,b) for(register int i=a,end##i=b;i>=end##i;--i)
#define go(i,x) for(int i=hed[x];i;i=e[i].pre)
using namespace std;
inline int read()
{
char x='\0';
int fh=1,sum=0;
for(x=getchar();x<'0'||x>'9';x=getchar())if(x=='-')fh=-1;
for(;x>='0'&&x<='9';x=getchar())sum=sum*10+x-'0';
return fh*sum;
}
const int N=4e6+9;
const double pi=acos(-1);
struct plx{
double x,y;
plx(double xx=0,double yy=0){x=xx,y=yy;}
friend plx operator+(plx a,plx b){return plx(a.x+b.x,a.y+b.y);}
friend plx operator-(plx a,plx b){return plx(a.x-b.x,a.y-b.y);}
friend plx operator*(plx a,plx b){return plx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
}a[N],b[N],wn[N];
int n,m,rev[N];
V getrev(int len)
{
FOR(i,0,len-1)wn[i]=plx(cos(pi/len*i),sin(pi/len*i));
FOR(i,0,len-1)rev[i]=((rev[i>>1]>>1)|((i&1)*(len>>1)));
}
V FFT(plx *A,int len,int tp)
{
FOR(i,0,len-1)if(i<rev[i])swap(A[i],A[rev[i]]);
for(int mid=1;mid<len;mid<<=1)
{
for(int i=0;i<len;i+=mid*2)
{
for(int j=0;j<mid;j++)
{
plx x=A[i+j],y=A[i+mid+j]*wn[len/mid*j];
A[i+j]=x+y,A[i+mid+j]=x-y;
}
}
}
if(tp==-1)
{
reverse(A+1,A+len);
FOR(i,0,len-1)A[i].x=(A[i].x>0)?(int)(A[i].x/len+0.5):(int)(A[i].x/len-0.5);
}
}
int main()
{
n=read()+1,m=read()+1;
FOR(i,0,n-1)a[i].x=read();
FOR(i,0,m-1)b[i].x=read();
int len=1;
while(len<n+m-1)len<<=1;
getrev(len);
FFT(a,len,1),FFT(b,len,1);
FOR(i,0,len-1)a[i]=a[i]*b[i];
FFT(a,len,-1);
FOR(i,0,n+m-2)printf("%d ",(int)a[i].x);
return 0;
}