mtt模板

4次FFT 1.21s

#include<bits/stdc++.h>
using namespace std;
#define forg(i,x) for(register int i=fir[x];i;i=nxt[i])
#define uu unsigned
#define scanf a14=scanf
#define rint register int
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
typedef long long ll;
typedef uu long long ull;
typedef pair<int,int>pii;
int a14;
inline int rd(int l,int r){return rand()%(r-l+1)+l;}

const int mxn=4e5+3;
int n,m,mod,N,R[mxn];
ll quz(double x){ll re=x>0?x+0.5:x-0.5;return re%mod;}
struct C{
    double r,i;C(double xx=0,double yy=0){r=xx,i=yy;}
    C operator+(const C&a)const{return C(r+a.r,i+a.i);}
    C operator-(const C&a)const{return C(r-a.r,i-a.i);}
    C operator*(const C&a)const{return C(r*a.r-i*a.i,r*a.i+i*a.r);}
    C operator*(double x)const{return C(r*x,i*x);}
    C operator/(double x)const{return C(r/x,i/x);}
    void operator+=(const C&a){r+=a.r,i+=a.i;}
    void operator-=(const C&a){r-=a.r,i-=a.i;}
    void operator*=(double x){r*=x,i*=x;}
    void operator/=(double x){r/=x,i/=x;}
}w[2][mxn],a0[mxn],a1[mxn],b0[mxn],b1[mxn],c2[mxn],c1[mxn];
const C I(0,1);const double PI=acos(-1);
void fft(C a[],int n,int tp=0){
    for(int i=0;i<n;++i)if(i>R[i])swap(a[i],a[R[i]]);
    for(int d=1,t=N>>1;t;d*=2,t>>=1){
        for(int i=0;i<n;i+=d*2)for(int j=0;j<d;++j){
            C tmp=a[i+j+d]*w[tp][j*t];
            a[i+j+d]=a[i+j]-tmp,a[i+j]+=tmp;
        }
    }
    if(tp)for(int i=0;i<n;++i)a[i]/=n;
}
void fft2(C a[],C b[],int n){
    for(int i=0;i<n;++i)a[i]+=I*b[i];
    fft(a,n);for(int i=0;i<n;++i)b[i?n-i:0]=a[i];for(int i=0;i<n;++i)b[i].i*=-1;
    for(int i=0;i<n;++i){C t1=a[i],t2=b[i];a[i]=(t1+t2)/2,b[i]=(t2-t1)/2*I;}
}
int main(){
    scanf("%d%d%d",&n,&m,&mod);int sq=sqrt(mod)+1;
    for(int i=0,x;i<=n;++i)scanf("%d",&x),x%=mod,a0[i]=C(x/sq),a1[i]=C(x%sq);for(int i=0,x;i<=m;++i)scanf("%d",&x),x%=mod,b0[i]=C(x/sq),b1[i]=C(x%sq);
    N=1;while(N<n+m+1)N*=2;for(int i=1;i<N;++i)R[i]=R[i>>1]>>1,R[i]|=(i&1)*(N>>1);
    for(int i=0;i<N/2;++i)w[0][i]=w[1][i]=C(cos(2*PI*i/N),sin(2*PI*i/N)),w[1][i].i*=-1;
    fft2(a0,a1,N),fft2(b0,b1,N);for(int i=0;i<N;++i)c1[i]=a0[i]*b0[i]+I*a1[i]*b0[i],c2[i]=a0[i]*b1[i]+I*a1[i]*b1[i];
    fft(c1,N,1),fft(c2,N,1);
    for(int i=0;i<=n+m;++i){ll re=sq*sq*quz(c1[i].r)+sq*(quz(c1[i].i)+quz(c2[i].r))+quz(c2[i].i);printf("%lld ",re%mod);}puts("");
    return 0;
}
posted @ 2021-06-25 19:30  yugyppah656  阅读(155)  评论(0编辑  收藏  举报