刚学了FFT,感觉迷迷糊糊的
FFT的主要思想:
函数表达 ---- 点表达 ----- 点值相乘 ----- 还原为函数
先留个坑,现在刚刚背完代码,以后学的时候再理解
#include<bits/stdc++.h>
#define N 5000050
using namespace std;
const double PI = acos(-1.0);
struct Comp{
double x,y;
Comp(double xx=0, double yy=0):x(xx),y(yy){};
}a[N],b[N];
Comp operator + (const Comp &a, const Comp &b){return Comp(a.x+b.x, a.y+b.y);}
Comp operator - (const Comp &a, const Comp &b){return Comp(a.x-b.x, a.y-b.y);}
Comp operator * (const Comp &a, const Comp &b){return Comp(a.x*b.x - a.y*b.y, a.x*b.y+a.y*b.x);}
// (a+bi)(c+di) = (ac-bd, (d+bc)i)
int n,m,lim=1,r[N];
void FFT(Comp *a, int rev){
for(int i=0;i<lim;i++) if(i<r[i]) swap(a[i], a[r[i]]);
for(int i=1;i<lim;i <<= 1){
Comp wn(cos(PI/i), rev * sin(PI/i));
for(int j=0;j<lim;j += (i<<1)){
Comp w(1,0);
for(int k=0;k<i;k++,w=w*wn){
Comp x = a[j+k], y = w * a[j+k+i];
a[j+k] = x + y;
a[j+k+i] = x - y;
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++) scanf("%lf",&a[i].x);
for(int i=0;i<=m;i++) scanf("%lf",&b[i].x);
int l = 0;
while(lim <= m+n) lim <<= 1, l++;
for(int i=0;i<=lim;i++) r[i] = (r[i>>1]>>1)|((i&1)<<(l-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;
}