传送门
N
O
I
p
NOIp
NOIp爆炸不能阻止我搞
o
i
oi
oi的决心
信息技术课进行一点康复训练。
f
f
t
fft
fft板题。
代码:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
const int N=4e5+5;
const double pi=acos(-1.0);
int n,m,lim=1,tim=0,pos[N];
struct Complex{
double x,y;
friend inline Complex operator+(const Complex&a,const Complex&b){return (Complex){a.x+b.x,a.y+b.y};}
friend inline Complex operator-(const Complex&a,const Complex&b){return (Complex){a.x-b.x,a.y-b.y};}
friend inline Complex operator*(const Complex&a,const Complex&b){return (Complex){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
}a[N],b[N];
inline void fft(Complex a[],int type){
for(int i=0;i<lim;++i)if(i<pos[i])swap(a[i],a[pos[i]]);
for(int mid=1;mid<lim;mid<<=1){
Complex w_n=(Complex){cos(pi/mid),type*sin(pi/mid)};
for(int j=0,len=mid<<1;j<lim;j+=len){
Complex w=(Complex){1,0};
for(int k=0;k<mid;++k,w=w*w_n){
Complex a0=a[j+k],a1=w*a[j+k+mid];
a[j+k]=a0+a1,a[j+k+mid]=a0-a1;
}
}
}
}
int main(){
n=read(),m=read();
for(int i=0;i<=n;++i)a[i].x=read();
for(int i=0;i<=m;++i)b[i].x=read();
while(lim<=n+m)lim<<=1,++tim;
for(int i=0;i<lim;++i)pos[i]=(pos[i>>1]>>1)|((i&1)<<(tim-1));
fft(a,1),fft(b,1);
for(int i=0;i<lim;++i)a[i]=a[i]*b[i];
fft(a,-1);
for(int i=0;i<=n+m;++i)printf("%d ",(int)(a[i].x/lim+0.5));
return 0;
}