跳到正文

个人记录

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

yqzx0307约 13 分钟
原文

这是 2026年8月4日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

FFT 是一种高效实现 DFT 的算法,称为快速傅立叶变换(Fast Fourier Transform,FFT).

它支持在 O(nlog⁡n)O(n\log n) 的时间内计算两个 nn 次多项式的乘法,比朴素的 O(n2)O(n^2) 算法更高效.由于两个整数的乘法也可以被当作多项式乘法,因此这个算法也可以用来加速大整数的乘法计算。

递归版
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn=1e7+10;
constexpr double pi=acos(-1.0);
constexpr double eps=1e-6;
int n,m;
int Fx[maxn],Gx[maxn];

typedef struct comp
{
    double Re,Im;
    comp()=default;
    comp(int a)
    {
        this->Re=a;
        this->Im=0;
    }
    comp(double x,double y)
    {
        this->Re=x;
        this->Im=y;
    }
    comp operator+(const comp& z)
    {
        return comp(Re+z.Re,Im+z.Im);
    }
    comp operator-(const comp& z)
    {
        return comp(Re-z.Re,Im-z.Im);
    }
    comp operator*(const comp& z)
    {
        return comp(Re*z.Re-Im*z.Im,Re*z.Im+Im*z.Re);
    }
    comp operator/(const int& r)
    {
        double R=r;
        return comp(Re/R,Im/R);
    }

    void operator*=(const comp& z)
    {
        (*this)=(*this)*z;
    }
    void operator/=(const int& r)
    {
        double R=r;
        (*this)=(*this)/R;
    }
}comp;//复数结构体

void prt(comp &z)
{
    printf("%.3lf %.3lf\n",z.Re,z.Im);
}//调试函数

comp tmp[maxn];
void DFT(comp *f,int n,int rev=1)
{
    if(n==1)
    {
        return ;
    }
    int half_n=(n>>1);
    for(int i=0;i<n;++i)
    {
        tmp[i]=f[i];
    }//准备奇偶重排

    for(int i=0;i<half_n;++i)
    {
        f[i]=tmp[i<<1];
        f[i+half_n]=tmp[i<<1|1];
    }//项数分离
    comp *F=f;
    comp *G=f+half_n;
    DFT(F,half_n,rev);//A[0]
    DFT(G,half_n,rev);//A[1]

    //开始拼接计算
    comp w_1=comp(cos(2*pi/n),rev*sin(2*pi/n));//单位根$w_n^1$
    comp w_k=comp(1,0);//$w_n^k$
    for(int i=0;i<half_n;++i)
    {
        tmp[i]=F[i]+w_k*G[i];//k<half_n
        tmp[i+half_n]=F[i]-w_k*G[i];//k>=half_n

        w_k*=w_1;
    }

    for(int i=0;i<n;++i)
    {
        f[i]=tmp[i];
    }
}

void IDFT(comp *f,int n)
{
    DFT(f,n,-1);
    for(int i=0;i<n;++i)
    {
        f[i]/=n;
    }
}

comp F_[maxn],G_[maxn];
void mul()
{
    for(int i=0;i<=n;++i)
    {
        F_[i]=comp(Fx[i]);
    }
    for(int i=0;i<=m;++i)
    {
        G_[i]=comp(Gx[i]);
    }

    int h_len=(1<<(__lg(n+m+1)+1));
    for(int i=n+1;i<h_len;++i)
    {
        F_[i]=comp(0);
    }
    for(int i=m+1;i<h_len;++i)
    {
        G_[i]=comp(0);
    }

    DFT(F_,h_len,1);
    DFT(G_,h_len,1);

    for(int i=0;i<h_len;++i)
    {
        F_[i]*=G_[i];//存入结果点值表达
    }
    IDFT(F_,h_len);//rev有初始值,可不加

    for(int i=0;i<h_len;++i)
    {
        Fx[i]=F_[i].Re+0.5;
    }
    for(int i=0;i<=n+m;++i)
    {
        printf("%lld ",Fx[i]);
    }
}
signed main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=0;i<=n;++i)
    {
        scanf("%lld",Fx+i);
    }
    for(int i=0;i<=m;++i)
    {
        scanf("%lld",Gx+i);
    }
    mul();
    return 0;
}

蝶形运算优化 已知 G(ωn/2k)G(\omega_{n/2}^k) 和H(ωn/2k)H(\omega_{n/2}^k) 后,需要使用下面两个式子求出 f(ωnk)f(\omega_n^k) 和 f(ωnk+n/2)f(\omega_n^{k+n/2}):

f(ωnk)=G(ωn/2k)+ωnk×H(ωn/2k)f(ωnk+n/2)=G(ωn/2k)−ωnk×H(ωn/2k)\begin{aligned} f(\omega_n^k) & = G(\omega_{n/2}^k) + \omega_n^k \times H(\omega_{n/2}^k) \\ f(\omega_n^{k+n/2}) & = G(\omega_{n/2}^k) - \omega_n^k \times H(\omega_{n/2}^k) \end{aligned} 使用位逆序置换后,对于给定的 n,kn, k:

G(ωn/2k)G(\omega_{n/2}^k) 的值存储在数组下标为 kk 的位置,H(ωn/2k)H(\omega_{n/2}^k) 的值存储在数组下标为k+n2 k + \dfrac{n}{2} 的位置.
f(ωnk)f(\omega_n^k) 的值将存储在数组下标为 kk 的位置,f(ωnk+n/2)f(\omega_n^{k+n/2}) 的值将存储在数组下标为 k+n2k + \dfrac{n}{2} 的位置.

因此可以直接在数组下标为 kk 和 k+n2k + \frac{n}{2} 的位置进行覆写,而不用开额外的数组保存值.此方法即称为 蝶形运算,或更准确的,基 - 2 蝶形运算.

以上内容摘自OI Wiki。

迭代版
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn=1e7+10;
constexpr double pi=acos(-1);
constexpr double eps=1e-6;
int Fx[maxn],Gx[maxn];
int tr[maxn];//存放蝴蝶变换的位逆序置换
int n,m;

typedef struct comp
{
    double re,im;
    comp()=default;
    comp(int a)
    {
        this->re=a;
        this->im=0;
    }
    comp(double x,double y)
    {
        this->re=x;
        this->im=y;
    }
    comp operator+(const comp& b)
    {
        return comp(re+b.re,im+b.im);
    }
    comp operator-(const comp& b)
    {
        return comp(re-b.re,im-b.im);
    }
    comp operator*(const comp& b)
    {
        return comp(re*b.re-im*b.im,re*b.im+b.re*im);
    }
    void operator*=(const comp& b)
    {
        (*this)=(*this)*b;
    }
    comp operator/(const int &b)
    {
        double db=b;
        return comp(re/db,im/db);
    }
    void operator/=(const int &b)
    {
        (*this)=(*this)/b;
    }
} comp;

comp F_[maxn],G_[maxn];

void prt(comp &a)
{
    printf("%.3lf %.3lf\n",a.re,a.im);
}

//迭代版DFT
void DFT(comp *f,int n,int rev=1)
{
    if(n==1)
    {
        return ;
    }
    //1.计算位逆序置换并原地交换
    int limit=__lg(n);
    for(int i=0;i<n;++i)
    {
        tr[i]=(tr[i>>1]>>1)|((i&1)<<(limit-1));
        if(i<tr[i])
        {
            swap(f[i],f[tr[i]]);
        }
    }
    //2.自底向上迭代(蝴蝶操作)
    for(int p=2;p<=n;p<<=1)//p为当前合并的区间长度
    {
        int half=p>>1;//len为半区间长度
        comp w_n=comp(cos(2*pi/p),rev*sin(2*pi/p));//单位根
        for(int k=0;k<n;k+=p)//遍历每个区间
        {
            comp w=comp(1,0);
            for(int l=k;l<k+half;++l)//遍历半区间
            {
                comp t=w*f[l+half];
                comp u=f[l];
                f[l]=u+t;
                f[l+half]=u-t;
                w*=w_n;
            }
        }
    }
}

void IDFT(comp *f,int n)
{
    DFT(f,n,-1);
    for(int i=0;i<n;++i)
    {
        f[i]/=n;
    }
}

void mul()
{
    for(int i=0;i<=n;++i)
    {
        F_[i]=comp(Fx[i]);
    }
    for(int i=0;i<=m;++i)
    {
        G_[i]=comp(Gx[i]);
    }
    int hlen=(1<<(__lg(n+m+1)+1));
    for(int i=n+1; i<hlen; ++i)
    {
        F_[i]=comp(0);
    }
    for(int i=m+1; i<hlen; ++i)
    {
        G_[i]=comp(0);
    }

    DFT(F_,hlen);
    DFT(G_,hlen);

    for(int i=0;i<hlen;++i)
    {
        F_[i]*=G_[i];
    }
    IDFT(F_,hlen);

    for(int i=0;i<hlen;++i)
    {
        Fx[i]=F_[i].re+0.5;
    }
    for(int i=0;i<=n+m;++i)
    {
        printf("%lld ",Fx[i]);
    }
}

signed main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=0; i<=n; ++i)
    {
        scanf("%lld",Fx+i);
    }
    for(int i=0; i<=m; ++i)
    {
        scanf("%lld",Gx+i);
    }
    mul();
    return 0;
}

https://www.luogu.com.cn/problem/P3803