有时间再修
\(\operatorname{FFT}\)
点击查看代码
#include<bits/stdc++.h>
#define cs const
#define il inline
#define fo(i,j,k) for(int i=(j);(i)<=(k);++(i))
#define of(i,j,k),for(int i=(j);(i)>=(k);--(i))
#define LL long long
using namespace std;
cs int N=3e6+9;
cs double pi=acos(-1);
int FL,CH;
template<typename T>bool in(T&a)
{
for(FL=1;!isdigit(CH)&&CH!=EOF;CH=getchar())
if(CH=='-')FL=-1;
for(a=0;isdigit(CH);CH=getchar())
a=a*10+CH-'0';
return a*=FL,CH==EOF?0:1;
}
template<typename T,typename...Args>
int in(T&a,Args&...args){return in(a)+in(args...);}
struct Complex
{
double x,y;
Complex operator+(const Complex &a) const
{ return {x+a.x,y+a.y}; }
Complex operator-(const Complex &a) const
{ return {x-a.x,y-a.y}; }
Complex operator*(const Complex &a) const
{ return {x*a.x-y*a.y,x*a.y+y*a.x}; }
}A[N],B[N];
int rev[N],bit,tot,n,m;
il void FFT(Complex a[],int inv)
{
fo(i,0,tot-1) if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int mid=1;mid<tot;mid<<=1)
{
Complex w1=(Complex){cos(pi/mid),inv*sin(pi/mid)};
for(int i=0;i<tot;i+=mid*2)
{
Complex wk=(Complex){1,0};
for(int j=0;j<mid;++j,wk=wk*w1)
{
Complex x=a[i+j],y=wk*a[i+j+mid];
a[i+j]=x+y,a[i+j+mid]=x-y;
}
}
}
}
signed main()
{
in(n,m);
fo(i,0,n) scanf("%lf",&A[i].x);
fo(i,0,m) scanf("%lf",&B[i].x);
while((1<<bit)<n+m+1) ++bit; //bit=ceil(log_2^n)
tot=1<<bit;
fo(i,0,tot-1) rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1));
FFT(A,1),FFT(B,1);
fo(i,0,tot-1) A[i]=A[i]*B[i];
FFT(A,-1);
fo(i,0,n+m) printf("%d ",(int)(A[i].x/tot+0.5));
return 0;
}