【学习笔记】FFT
1、内容
由于noble_太懒 不想写了
非常好的博客:
https://www.cnblogs.com/rvalue/p/7351400.html
http://www.cnblogs.com/candy99/p/6641972.html
http://www.gatevin.moe/acm/fft%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/
http://hzwer.com/6896.html 黄学长模板
https://blog.csdn.net/ggn_2015/article/details/68922404
http://www.cnblogs.com/zwfymqz/p/8244902.html
http://www.cnblogs.com/19992147orz/p/8041323.html
2、模板
洛谷A了,maxn要开大一点
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef complex<double> com; 4 const int maxn=3e7; 5 const double PI=acos(-1); 6 com a[maxn], b[maxn]; 7 int rev[maxn]; 8 9 void FFT(com* a,int n,int type){ 10 for(int i=0;i<n;i++){ 11 if(rev[i]>i) swap(a[i],a[rev[i]]); 12 } 13 14 for(int step=1;step<n;step<<=1){ //待合并区域中点 15 com wn(cos(PI/step),type*sin(PI/step)); 16 for(int j=0;j<n;j+=(step<<1)){ //step<<1是区间右端点 17 com w(1,0); //幂 18 for(int k=j;k<j+step;k++,w*=wn){//枚举左半部分 19 com x=a[k], y=w*a[k+step]; 20 a[k]=x+y; a[k+step]=x-y; 21 } 22 } 23 } 24 // if(type==-1) for(int i=0;i<n;i++) a[i]/=n; 25 } 26 int main() 27 { 28 int n1,n2,n,x,L=0; 29 scanf("%d%d",&n1,&n2); 30 for(int i=0;i<=n1;i++){ 31 scanf("%d",&x); a[i]=x; 32 } 33 for(int i=0;i<=n2;i++){ 34 scanf("%d",&x); b[i]=x; 35 } 36 for(n=1;n<=n1+n2;n*=2) L++; 37 for(int i=0;i<n;i++){ 38 rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1)); 39 } 40 FFT(a,n,1); FFT(b,n,1); 41 for(int i=0;i<=n;i++) a[i]*=b[i]; 42 FFT(a,n,-1); 43 for(int i=0;i<=n1+n2;i++){ 44 printf("%d ",(int)(a[i].real()/n+0.5)); 45 } 46 return 0; 47 }