BZOJ 2194 快速傅立叶之二

思路

把b反向,就变成卷积的形式了

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
const int MOD = 2281701377;
const int G = 3;
const int invG = 760567126;
const int MAXN = 300000;
int rev[MAXN];
int pow(int a,int b){
    int ans=1;
    while(b){
        if(b&1)
            ans=(1LL*ans*a)%MOD;
        a=(1LL*a*a)%MOD;
        b>>=1;
    }
    return ans;
}
void cal_rev(int n,int lim){
    for(int i=0;i<n;i++)
        rev[i]=(rev[i>>1]>>1)|((i&1)<<(lim-1));
}
void NTT(int *a,int opt,int n,int lim){
    for(int i=0;i<n;i++)
        if(i<rev[i])
            swap(a[i],a[rev[i]]);
    for(int i=2;i<=n;i<<=1){
        int len=i/2,tmp=pow((opt)?G:invG,(MOD-1)/i);
        for(int j=0;j<n;j+=i){
            int arr=1;
            for(int k=j;k<j+len;k++){
                int t=(1LL*a[k+len]*arr)%MOD;
                a[k+len]=(a[k]-t+MOD)%MOD;
                a[k]=(a[k]+t)%MOD;
                arr=(1LL*arr*tmp)%MOD;
            }
        }
    }
    if(!opt){
        int invN = pow(n,MOD-2);
        for(int i=0;i<n;i++)
            a[i]=(1LL*a[i]*invN)%MOD;
    }
}
int n,a[MAXN],b[MAXN];
signed main(){
    scanf("%lld",&n);
    for(int i=0;i<n;i++)    
        scanf("%lld %lld",&a[i],&b[i]);
    reverse(b,b+n);
    int midlen=1,midlim=0;
    while(midlen<=(2*n))
        midlen<<=1,midlim++;
    cal_rev(midlen,midlim);
    NTT(a,1,midlen,midlim);
    NTT(b,1,midlen,midlim);
    for(int i=0;i<midlen;i++)
        a[i]=(a[i]*b[i])%MOD;
    NTT(a,0,midlen,midlim);
    for(int i=n-1;i<2*n-1;i++){
        printf("%lld\n",a[i]);
    }
    return 0;
}
posted @ 2019-04-25 07:29  dreagonm  阅读(131)  评论(0编辑  收藏  举报