为什么FFT拆了系数还是精度不行啊

P4245 【模板】任意模数多项式乘法

JS_TZ_ZHR @ 2022-02-05 23:56:52

RT

#include<bits/stdc++.h>
#define N 400005
#define int long long
using namespace std;
int n,m,sum[N<<1],num,p,tmp,sum1,sum2;
long long ans;
long double Pi=acos(-1.0);
struct node {
    long double x,y;
    node(long double _x=0,long double _y=0) {
        x=_x,y=_y;
    }
} a1[N],b1[N],a2[N],b2[N],res1[N],res2[N],res3[N];
node operator+(node a,node b) {
    return node(a.x+b.x,a.y+b.y);
}
node operator-(node a,node b) {
    return node(a.x-b.x,a.y-b.y);
}
node operator*(node a,node b) {
    return node(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
}
inline void FFT(int len,node *s,int opt) {
    for(int i=0; i<len; i++)if(i<sum[i])swap(s[i],s[sum[i]]);
    for(int i=1;i<len;i<<=1){
        node W=node(cos(Pi/(long double)i),(long double)opt*sin(Pi/(long double)i));
        for(int j=0;j<len;j+=(i<<1)){
            node w=node(1.0,0.0);
            for(int k=0;k<i;k++,w=w*W){
                node t1=w*s[i+j+k],t2=s[j+k];
                s[j+k]=t2+t1;
                s[i+j+k]=t2-t1;
            }
        }
    }
}
signed main() {
    cin>>n>>m>>p;
    for(int i=0; i<=n; i++){
        cin>>tmp;
        a2[i].x=tmp/66536;
        a1[i].x=tmp%66536;
    }
    for(int i=0; i<=m; i++){
        cin>>tmp;
        b2[i].x=tmp/66536;
        b1[i].x=tmp%66536;
    }
    int len=1;
    while(len<=n+m)len<<=1,num++;
    for(int i=0; i<len; i++)sum[i]=(sum[i>>1]>>1)|((i&1)<<(num-1));
    FFT(len,a1,1);
    FFT(len,b1,1);
    FFT(len,a2,1);
    FFT(len,b2,1);
    for(int i=0;i<=len;i++){
        res1[i]=a2[i]*b2[i];
        res2[i]=a1[i]*b2[i]+a2[i]*b1[i];
        res3[i]=a1[i]*b1[i];
    }
    FFT(len,res1,-1);
    FFT(len,res2,-1);
    FFT(len,res3,-1);
    sum1=(66536ll*66536ll)%p;
    sum2=66536%p;
    for(int i=0;i<=n+m;i++){
        res1[i].x/=len,res2[i].x/=len,res3[i].x/=len;
        ans=res1[i].x*sum1+res2[i].x*sum2+res3[i].x+0.5;
        ans%=p;
        cout<<(ans+p)%p<<' ';
    }
}

by NaCly_Fish @ 2022-02-06 00:56:33

@JS_TZ_ZHR 四次 fft 不香吗(


|