用了个小优化,卷积时把实部虚部初值赋为两个要卷的多项式,最后得到答案虚部/2即为所求。
//#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <limits>
#include <map>
#include <vector>
#include <queue>
#define LL int
#define ft first
#define sd second
#define mp(x,y) make_pair(x,y)
#define db double
//#define int LL
using namespace std;
const int N = 1<<21;
//const int mod = ;
const int INF =numeric_limits<int >::max();
const db Pi=acos(-1.);
#define rep(i,x,y) for (int i=x;i<=y;++i)
void read(int &x)
{
x=0;
char ch=getchar();
int f=1;
while (!isdigit(ch)) (ch=='-'?f=-1:0),ch=getchar();
while ( isdigit(ch)) x=x*10+ch-'0',ch=getchar();
x*=f;
}
int n,m,A[N],B[N];
LL C[N];
struct Com
{
db x,y;
Com(db x=0.0,db y=0.0) :x(x),y(y){}
Com operator - (const Com &b) const {return Com(x-b.x,y-b.y);}
Com operator + (const Com &b) const {return Com(x+b.x,y+b.y);}
Com operator * (const Com &b) const {return Com(x*b.x-y*b.y,x*b.y+y*b.x);}
};
int rev[N<<1];
void FFT(Com *a,int n,int f)
{
rep(i,0,n-1) if (i>rev[i]) swap(a[i],a[rev[i]]);
for (int i=1;i<n;i<<=1)
{
Com wn=Com(cos(Pi/i),sin(Pi/i)),x,y;
for (int j=0;j<n;j+=(i<<1))
{
Com w=Com(1,0);
for (int k=0;k<i;++k,w=w*wn)
{
x=a[j+k];y=a[i+j+k]*w;
a[j+k]=x+y;
a[i+j+k]=x-y;
}
}
}
if (f==1) return ;reverse(a+1,a+n);
rep(i,0,n-1) a[i].x/=n,a[i].y/=n;
}
static Com a[N<<1];
void cal(int *A,int *B,int n,int m,int *C)
{
int R=n+m,L=1,g=0;
rep(i,0,R) a[i]=Com(A[i],B[i]);
for (;L<=R;L<<=1,++g);
//rep(i,R+1,L) a[i]=Com(0,0);
rep(i,0,L-1) rev[i]=(rev[i>>1]>>1)|(i&1)<<g-1;
FFT(a,L,1);
rep(i,0,L-1) a[i]=a[i]*a[i];
FFT(a,L,-1);
rep(i,0,R) C[i]=(LL)(a[i].y/2.0+0.5);
}
signed main()
{
///freopen("data.txt","r",stdin);
//freopen("Moon.txt","w" ,stdout);
read(n);read(m);
rep(i,0,n) read(A[i]);
rep(i,0,m) read(B[i]);
cal(A,B,n,m,C);
n+=m;
rep(i,0,n) printf("%d ",C[i]);
return 0;
}