FFT

· · 算法·理论

FFT

模板

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

Begin

考虑暴力,就有暴力的 O(nm) 做法。

换种思路:

:::info[引理:n 次多项式上至少 n+1 个不同的点可以确定一个唯一的 n 次多项式。]{close} 不够严谨证明:令 n 次多项式的各个系数为未知数。就有 n+1 个未知数;至少 n+1 个点,就有至少 n+1 个方程。k 个未知数需要被解出正常至少要 k 个方程(可以考虑高斯消元过程)。 :::

然后呢,我们找 N 个横坐标 x_1, x_2, \dots, x_N(注意是大写 N)。

在 A 与 B 上分别对应 N 个点,注意 A 上的点要与 B 上的点一一对应。

显然有 C(x_i) = A(x_i) \cdot B(x_i)。

由此可以得到 C 上的 N 个点,继而确定 C。

当然,n+m+1 \le N。

我们令 N = 最小的 2^i 且满足 n+m+1 \le N。

现在有两个问题:

Q1

我会暴力!

具体的,找任意 N 个横坐标。然后对于每一个横坐标在 A 与 B 上求出纵坐标。

对于每一个横坐标在 A 与 B 上求出纵坐标 是 O(n) 的(假设 n,m 同阶)。

所以总共是 O(Nn) 的,不如暴力。

这好像很难优化。

但注意到刚刚是“找任意 N 个横坐标”,能不能找一些特殊的值,利用它们的性质优化复杂度呢?

有的兄弟有的。

:::info[单位根]{close} 接下来引入单位根,那么要知道复数与弧度制(假设已知道弧度制)。

定义复数为 a+bi(i = \sqrt{-1})。

将复数丢进坐标轴中,横轴为 a,纵轴为 b。

单位根:

考虑 x^N = 1 这样的等式,那么有 N 个解(为什么等等证),称这些解为单位根(N 还是上文的 N)。

我们在坐标轴(复数意义下的坐标轴)以原点为圆心半径为 1 做圆。把圆 N 等分。这些绿色的交点共有 N 个,就是单位根。

设逆时针的第 i 个点(i 在 [0,N) 范围内)的复数表示为 ω_N^k,其中,横轴右半边的点为 ω_N^0(即 1)。

总结一下:

由第一个式子可以得到单位根很多优美性质:

补充:观察第一个性质,在坐标系中,相当于 k 这个点逆时针旋转 j 到 k+j 这个点,而第 k 点可以看作第 0 点逆时针旋转 k 到 k 这个点。

这下,就会证明:x^N = 1 这样的等式,那么至少有 N 个解:{(ω_N^k)}^N = ω_N^{kN},相当于 0 这个点逆时针转 kN 转回了 0 这个点,而 {(ω_N^0)}^N = 1^N = 1。

我们把 N 个横坐标变成对于 N 的单位根,利用他们的性质分别在 A,B 上找到 N 个点。

现在考虑 A,B 同理。

将 A 拆成偶数次项(包括常数)与奇数次项。

具体的,定义:

则 A(x) = O(x^2) + x \cdot E(x^2)。

将 x 换成 ω_N^{k}:

A(ω_N^{k}) = O({(ω_N^k)}^2) + ω_N^{k} \cdot E({(ω_N^k)}^2) = O(ω_{\frac{N}{2}}^k) + ω_N^{k} \cdot E(ω_{\frac{N}{2}}^k)

精妙的,利用性质,

A(ω_N^{k+\frac{N}{2}}) = O({(-ω_N^k)}^2) - ω_N^{k} \cdot E({(-ω_N^k)}^2) = O(ω_{\frac{N}{2}}^k) - ω_N^{k} \cdot E(ω_{\frac{N}{2}}^k)

总结一下:

那么,我们若求出 O(ω_{\frac{N}{2}}^k) 与 E(ω_{\frac{N}{2}}^k),就可以得出 A(ω_N^{k}) 与 A(ω_N^{k+\frac{N}{2}}) 这两个我们希望求出的值。

再回到 E 与 O 的本质:

这实际上与 A 形式相同,都是多项式!只不过次数减半。

那我们求 O(x) 与 E(x) 就像求 A(x) 一样,就是一个递归过程,终止条件是只有常数项。

那么我们多项式求 N 个单位根的坐标变成了多项式求 N 个单位根的坐标,但次数减半,层数是 \log 的!

这就把 Q1 优化到 O(n \log n)。

Q2

将 A,B 点对应相乘得到 C 的点后,考虑怎么把点变回系数。

对于矩阵 A,回归本质,Q1 的暴力用矩阵表示:

\begin{bmatrix} A(ω_N^0) \\ A(ω_N^1) \\ \vdots \\ A(ω_N^{N-1}) \end{bmatrix} = \begin{bmatrix} 1 & ω_N^0 & (ω_N^0)^2 & \cdots & (ω_N^0)^{N-1} \\ 1 & ω_N^1 & (ω_N^1)^2 & \cdots & (ω_N^1)^{N-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{N-1} & (ω_N^{N-1})^2 & \cdots & (ω_N^{N-1})^{N-1} \end{bmatrix} \times \begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_{N-1} \end{bmatrix}

化简:

\begin{bmatrix} A(ω_N^0) \\ A(ω_N^1) \\ \vdots \\ A(ω_N^{N-1}) \end{bmatrix} = \begin{bmatrix} 1 & ω_N^{0 \cdot 1} & ω_N^{0 \cdot 2} & \cdots & ω_N^{0 \cdot (N-1)} \\ 1 & ω_N^{1 \cdot 1} & ω_N^{1 \cdot 2} & \cdots & ω_N^{1 \cdot (N-1)} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{(N-1) \cdot 1} & ω_N^{(N-1) \cdot 2} & \cdots & ω_N^{(N-1) \cdot (N-1)} \end{bmatrix} \times \begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_{N-1} \end{bmatrix}

移项(现在系数是未知的):

\begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_{N-1} \end{bmatrix} = \begin{bmatrix} 1 & ω_N^{0 \cdot 1} & ω_N^{0 \cdot 2} & \cdots & ω_N^{0 \cdot (N-1)} \\ 1 & ω_N^{1 \cdot 1} & ω_N^{1 \cdot 2} & \cdots & ω_N^{1 \cdot (N-1)} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{(N-1) \cdot 1} & ω_N^{(N-1) \cdot 2} & \cdots & ω_N^{(N-1) \cdot (N-1)} \end{bmatrix}^{-1} \times \begin{bmatrix} A(ω_N^0) \\ A(ω_N^1) \\ \vdots \\ A(ω_N^{N-1}) \end{bmatrix}

逆矩阵的具体表达式:根据定理,上述矩阵的逆矩阵等于 \frac{1}{N} 乘以将每个 ω_N^{k \cdot j} 替换为 ω_N^{-k \cdot j} 得到的矩阵。即逆矩阵的第 k 行第 j 列元素为 \frac{1}{N} ω_N^{-k \cdot j}(k,j 从 0 到 N-1)。不会证。

写成矩阵形式:

\frac{1}{N} \begin{bmatrix} 1 & ω_N^{-0 \cdot 1} & ω_N^{-0 \cdot 2} & \cdots & ω_N^{-0 \cdot (N-1)} \\ 1 & ω_N^{-1 \cdot 1} & ω_N^{-1 \cdot 2} & \cdots & ω_N^{-1 \cdot (N-1)} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{-(N-1) \cdot 1} & ω_N^{-(N-1) \cdot 2} & \cdots & ω_N^{-(N-1) \cdot (N-1)} \end{bmatrix}

因此最终有:

\begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_{N-1} \end{bmatrix} = \frac{1}{N} \begin{bmatrix} 1 & ω_N^{-0 \cdot 1} & ω_N^{-0 \cdot 2} & \cdots & ω_N^{-0 \cdot (N-1)} \\ 1 & ω_N^{-1 \cdot 1} & ω_N^{-1 \cdot 2} & \cdots & ω_N^{-1 \cdot (N-1)} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{-(N-1) \cdot 1} & ω_N^{-(N-1) \cdot 2} & \cdots & ω_N^{-(N-1) \cdot (N-1)} \end{bmatrix} \times \begin{bmatrix} A(ω_N^0) \\ A(ω_N^1) \\ \vdots \\ A(ω_N^{N-1}) \end{bmatrix} )

把 Q1 的矩阵和 Q2 的放在一起:

\begin{bmatrix} A(ω_N^0) \\ A(ω_N^1) \\ \vdots \\ A(ω_N^{N-1}) \end{bmatrix} = \begin{bmatrix} 1 & ω_N^{0 \cdot 1} & ω_N^{0 \cdot 2} & \cdots & ω_N^{0 \cdot (N-1)} \\ 1 & ω_N^{1 \cdot 1} & ω_N^{1 \cdot 2} & \cdots & ω_N^{1 \cdot (N-1)} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{(N-1) \cdot 1} & ω_N^{(N-1) \cdot 2} & \cdots & ω_N^{(N-1) \cdot (N-1)} \end{bmatrix} \times \begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_{N-1} \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_{N-1} \end{bmatrix} = \frac{1}{N} \begin{bmatrix} 1 & ω_N^{-0 \cdot 1} & ω_N^{-0 \cdot 2} & \cdots & ω_N^{-0 \cdot (N-1)} \\ 1 & ω_N^{-1 \cdot 1} & ω_N^{-1 \cdot 2} & \cdots & ω_N^{-1 \cdot (N-1)} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & ω_N^{-(N-1) \cdot 1} & ω_N^{-(N-1) \cdot 2} & \cdots & ω_N^{-(N-1) \cdot (N-1)} \end{bmatrix} \times \begin{bmatrix} A(ω_N^0) \\ A(ω_N^1) \\ \vdots \\ A(ω_N^{N-1}) \end{bmatrix} )

注意观察,这个 Q2 形式,与 Q1 本质相同,就是多了个系数。我们可以用 Q1 方法做 Q2。

省流(总结)

_{A系数 -(FFT)-> A点值}^{B系数 -(FFT)-> B点值}-(点乘)-> C点值 -(IFFT)-> C系数

Code

#include<bits/stdc++.h>
using namespace std;
const double PI=3.141592653589793;
inline int read() {
    int x=0,a=getchar();
    while (a<48||a>57) a=getchar();
    while (48<=a&&a<=57) x=(x<<3)+(x<<1)+a-48,a=getchar();
    return x;
}

int n,m,N_,N=2;
struct Complex {
    double x,y;
    Complex(double x_=0,double y_=0) {x=x_,y=y_;}
} f[3000006],g[3000006],tmp[3000006];
Complex operator - (Complex a,Complex b) {return Complex(a.x-b.x,a.y-b.y);}
Complex operator + (Complex a,Complex b) {return Complex(a.x+b.x,a.y+b.y);}
Complex operator * (Complex a,Complex b) {return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}

void FFT(Complex *p,int N,int op) {
    if (N==1) return;
    for (int i=0; i<N; i++) tmp[i]=p[i];
    int mid=N>>1;
    Complex *o=p,*e=p+mid;
    for (int i=0; i<mid; i++) o[i]=tmp[i<<1],e[i]=tmp[i<<1|1];
    FFT(o,mid,op),FFT(e,mid,op);
    Complex x=Complex(1,0),d;
    Complex x_1=Complex(cos(op*2*PI/N),sin(op*2*PI/N));
    for (int i=0; i<mid; i++)
        d=x*e[i],tmp[i]=o[i]+d,tmp[i+mid]=o[i]-d,x=x*x_1;
    for (int i=0; i<N; i++) p[i]=tmp[i];
}

signed main() {
    n=read(),m=read(),N_=n+m+1;
    while (N<N_) N<<=1;
    for (int i=0; i<=n; i++) f[i].x=read();
    for (int i=0; i<=m; i++) g[i].x=read();
    FFT(f,N,1);
    FFT(g,N,1);
    for (int i=0; i<N; i++) f[i]=f[i]*g[i];
    FFT(f,N,-1);
    for (int i=0; i<N_; i++) printf("%d ",(int)(f[i].x/N+0.5));

    return 0;
}

优化

我们将递归版的过程画图,显然是一个完全二叉树。 将分给 l 的边标记为 0,将分给 r 的边标记为 1。

观察走到底得到的编号与对应的原系数编号(都二进制下),刚好是头尾倒置。

:::info[为什么]{close} 对于一个多项式,一个系数被分到左边当且仅当她在这个多项式里充当偶次项的系数,也就是当前编号二进制下末位为 0(即为 2 的倍数);而右边相反。

边决定了这个系数二进制末位,而这个系数分流后,二进制末位会被消除(就是除以 2)。

也就是说在二进制下,往下走的过程中,边从低位向高位决定最后对应的原系数编号,因此是头尾倒置。
从下往上合并即可规避递归。
#### Code(已卡常)
```cpp
#include<bits/stdc++.h>
using namespace std;
constexpr double PI=3.141592653589793;
inline int read() {
int x=0,a=getchar();
while (a<48
while (48<=a&&a<=57) x=(x<<3)+(x<<1)+a-48,a=getchar();
return x;
}

int n,m,N_,N=2,Nn; complex<double> f[3000006],g[3000006]; int q[3000006];

inline void FFT(complex<double> p,int op) { for (int i=0;i<N;i++) if (i<q[i]) swap(p[i],p[q[i]]); for (int len=2,len=1;len<=N;len=len,len<<=1){ complex<double> x_(cos(op2PI/len),sin(op2PI/len)),o,e; for (int l=0;l<N;l+=len){ complex<double> x(1,0); for (int i=0;i<len_;i++){ o=p[l+i],e=xp[l+i+len]; p[l+i]=o+e,p[l+i+len]=o-e; x=x*x_; } } } }

signed main() { n=read(),m=read(),N=n+m+1; while (N<N) N<<=1,Nn++; for (int i=0;i<N;i++) q[i]=(q[i>>1]>>1)|((i&1)<<Nn); for (int i=0; i<=n; i++) f[i].real(read()); for (int i=0; i<=m; i++) g[i].real(read()); FFT(f,1); FFT(g,1); for (int i=0; i<N; i++) f[i]=f[i]*g[i]; FFT(f,-1); for (int i=0; i<N_; i++) printf("%d ",(int)(f[i].real()/N+0.5));

return 0;

}