FFT目害学笔记

2021-09-15 08:04:14  阅读：16  来源： 互联网

我的YY理解

FFT可以处理形如:

$h(x)=g(x)f(x)->h(x)=\sum\limits_{i=0}^{n}\sum\limits_{j=0}^{m}g_if_jx^{i+j}=\sum\limits_{i=0}^{n+m}\sum\limits_{j=0}^{i}g_jf_{i-j}x^i$

多项式乘法

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
namespace EMT{
typedef long long ll;typedef double db;//(double)clock() / (double)CLOCKS_PER_SEC;
#define pf printf
#define F(i,a,b) for(register int i=a;i<=b;i++)
#define D(i,a,b) for(register int i=a;i>=b;i--)
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
inline void file(){freopen("in.in","r",stdin);freopen("my.out","w",stdout);}
inline int max(int a,int b){return a>b?a:b;}inline int min(int a,int b){return a<b?a:b;}
inline void pi(int x){pf("%d ",x);}inline void pn(){pf("\n");}
const int N=1e7+10;
const db PI=acos(-1.0);
struct comp{
db x,y;
friend comp operator +(comp a,comp b){return (comp){a.x+b.x,a.y+b.y};}
friend comp operator -(comp a,comp b){return (comp){a.x-b.x,a.y-b.y};}
friend comp operator *(comp a,comp b){return (comp){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
}a[N],b[N];
int lim=1,n,m,ans[N],l,r[N];
inline void fft(comp *a,int tp){
F(i,0,lim-1)if(i<r[i])std::swap(a[i],a[r[i]]);
for(int mid=1;mid<lim;mid<<=1){
comp wn=(comp){cos(PI/mid),tp*sin(PI/mid)};
for(int R=mid<<1,j=0;j<lim;j+=R){
comp w=(comp){1,0};
for(int k=0;k<mid;k++,w=w*wn){
comp x=a[j+k],y=w*a[j+mid+k];
a[j+k]=x+y;
a[j+mid+k]=x-y;
}
}
}
}
inline short main(){
while(lim<=n+m)lim<<=1,l++;
F(i,0,lim-1)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
fft(a,1),fft(b,1);
F(i,0,lim)a[i]=a[i]*b[i];
fft(a,-1);
F(i,0,n+m)pi((int)(a[i].x/lim+0.5));
return 0;
}
}
signed main(){return EMT::main();}


快速傅立叶之二

$C_k=\sum\limits_{i=k}^{n}a_i\times b_{n+k-i}$

$$a_{n+1}$$～$$a_{n+k}$$都是0

$C_k=\sum\limits_{i=0}^{n+k}a_i \times b_{n+k-i}$

$D_k=\sum\limits_{i=0}^{k}a_i \times b_{k-i}$

$C_k=D_{n+k}$