2018.08.28 洛谷P3803 【模板】多项式乘法(FFT)

传送门
fft模板题。
终于学会fft了。
这个方法真是神奇!
经过试验发现手写的complex快得多啊!
代码:

#include
#include
#include
#define N 10000005
using namespace std;
inline int read(){
    int ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans*w;
}
inline void write(int x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar((x%10)^48);
}
const double pi=acos(-1.0);
struct complex{double x,y;inline complex(double xx=0,double yy=0){x=xx,y=yy;}}a[N],b[N];
inline complex operator+(complex a,complex b){return complex(a.x+b.x,a.y+b.y);}
inline complex operator-(complex a,complex b){return complex(a.x-b.x,a.y-b.y);}
inline complex operator*(complex a,complex b){return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
int n,m,c[N<<1],lim=1,Len,rev[N];
inline void fft(complex A[],int type){
    for(int i=0;iif(ifor(int mid=1;mid1){
        complex W_n(cos(pi/mid),type*sin(pi/mid));
        for(int len=mid<<1,j=0;jcomplex W(1,0);
            for(int k=0;kcomplex xx=A[j+k],yy=W*A[j+k+mid];
                A[j+k]=xx+yy,A[j+k+mid]=xx-yy;
            }
        }
    }
}
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<=m+n)lim<<=1,++Len;
    for(int i=0;i>1]>>1)|((i&1)<<(Len-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;
}

你可能感兴趣的:(#,fft)