Polynomial Multiplication & Fast Fourier Transform
Overview
In informatics competitions, polynomial multiplication shows up very, very often. The naive algorithm has time complexity \(O(n^2)\), which has made it the place where many nasty problem setters fail solutions on the exponent. So it is very necessary to use the Fast Fourier Transform (FFT) to optimize polynomial multiplication. Next, going from the shallow to the deep, I will introduce these two and the relationship between them.
Polynomials
A polynomial is generally defined as \(A(x) = \sum_{i = 0}^{n - 1} a_i x^i\), where \(x\) is the variable. The highest power in a polynomial is that polynomial's degree. Generally, a polynomial has two forms of representation:
- Coefficient representation
- Point-value representation
Coefficient representation
\(A(x) = a_0 + a_1 x^1 + a_2 x^2 + \dots a_{n-1} x^{n-1}\) can be abstracted as a vector \((a_0, a_1, a_2, a_3, \dots a_{n-1})\). Every vector corresponds one-to-one with a polynomial, and, since the coefficient representation is what is generally used for computation, for polynomial multiplication we basically require the vectors in coefficient representation as the input and output.
Point-value representation
For a polynomial, given \(n\) values \(x_0, x_1, x_2, \dots, x_{n-1}\), substituting them into \(A(x)\) to get \(y_0, y_1, y_2, \dots, y_{n-1}\) determines a polynomial of degree \(n-1\). The way of converting from the point-value representation to the coefficient representation is called interpolation.
Polynomial multiplication
Polynomial multiplication is done differently under the two representations:
- Coefficient representation:
\[A(x) \times B(x) = \sum_{k = 0}^{2n-2} x^k \sum_{x = 0}^k a_i b_{k - i} \]
Implemented this way, the time complexity is \(O(n^2)\), which is obviously quite high. - Point-value representation: \[ A(x_i) \times B(x_i) = A(x_i)B(x_i) \] The complexity is \(O(n)\).
Fast Fourier Transform
In informatics, the Fast Fourier Transform can be used to speed up convolution. In this article, I will introduce one kind of convolution, namely polynomial multiplication.
If we want to use the Fast Fourier Transform to optimize polynomial multiplication, we first need to understand the rough procedure. The general procedure for speeding up polynomial multiplication with FFT is:
- Convert the coefficient representation into the point-value representation; this is the part we optimize with FFT.
- Then compute the polynomial product under the point-value representation in \(O(n)\).
- Use IFT, that is, the inverse Fast Fourier Transform, to restore the point-value representation to the coefficient representation.
The code for FFT and IFT is almost the same, and both have complexity \(O(n \log n)\), so the overall complexity is \(O(n \log n)\).
Now let's formally start explaining the prerequisites for the Fast Fourier Transform.
Prerequisites
Imaginary numbers within complex numbers
The complex field is currently the largest number field. A complex number can be split into two parts, the real part and the imaginary part, written as:
\[ n = a + bi, i = \sqrt{-1} \]
Here, \(a\) is the real part and \(bi\) is the imaginary part. Any complex number can be represented on the complex plane:

Roots of unity
On the complex plane, just like when learning trigonometric functions, we can draw a unit circle with radius \(1\). Here, after dividing this circle into \(n\) equal parts, the complex numbers represented by these points are called roots of unity. Here, the roots of unity can be written as
\[\omega_n^k, k \in [0, n) \]
By Euler's formula, we have:
\[ e^{i\theta} = \sin \theta + i \cos \theta \]
Properties of roots of unity
Many properties of roots of unity are just like the ones in trigonometric functions. Here are a few:
- Property 1 \[ \omega_n^{kn} = \omega_n^0 \]
- Property 2 \[ \omega_n^k = \omega_{\frac{n}{2}}^{\frac{k}{2}} \]
- Property 3 \[ \omega_n^{k + \frac{n}{2}} = -\omega_n^k \]
The exponents of roots of unity obey the normal rules of arithmetic, which shows in the fact that they can be written as powers of \(e\).
Deriving the Fast Fourier Transform
We now know the coefficient representation of \(A(x)\), and now we want to convert it into the point-value representation. Normally, this substitution process is \(O(n^2)\). However, through the properties of complex numbers, we can quickly discover some hidden tricks.
For convenience, we now stipulate \(n = 2^x\). Consider splitting \(A(x)\) into two groups, odd and even:
\[ \begin{gathered} \begin{cases} A_1(x) = a_0 + a_2 x + a_4 x^2 + \dots + a_{n - 2}^{\frac{n}{2} - 1} \\ A_2(x) = a_1 + a_3 x + a_5 x^2 + \dots + a_{n - 1}^{\frac{n}{2} - 1} \end{cases} \\ A(x) = A_1(x^2) + x A_2(x^2) \end{gathered} \]
We now want to compute \( \sum_{k = 0}^{n - 1} A(\omega_n^{k}) \). Consider splitting this sum apart into two parts:
\[ \sum_{k = 0}^{\frac{n}{2} - 1} A(\omega_n^k) + \sum_{k = 0}^{\frac{n}{2} - 1} A(\omega_n^{k + \frac{n}{2}}) \]
First, let's compute the first part:
\[ \begin{aligned} A(\omega_n^k) &= A_1(\omega_n^{2k}) + \omega_n^k A_2(\omega_n^{2k}) \\ &= A_1(\omega_{\frac{n}{2}}^k) + \omega_n^k A_2(\omega_{\frac{n}{2}}^k) \end{aligned} \]
The second part:
\[ \begin{aligned} A(\omega_n^{k + \frac{n}{2}}) &= A_1(\omega_n^{2k + n}) + \omega_n^{k + \frac{n}{2}} A_2(\omega_n^{2k + n}) \\ &= A_1(\omega_n^{2k} \times \omega_n^n) - \omega_n^k A_2(\omega_n^{2k} \times \omega_n^n) \\ &= A_1(\omega_{\frac{n}{2}}^k) - \omega_n^k A_2(\omega_{\frac{n}{2}}^k) \end{aligned} \]
Comparing the results of the derivation:
\[ \begin{cases} \begin{aligned} A(\omega_n^k) &= A_1(\omega_{\frac{n}{2}}^k) + \omega_n^k A_2(\omega_{\frac{n}{2}}^k) \\ A(\omega_n^{k + \frac{n}{2}}) &= A_1(\omega_{\frac{n}{2}}^k) - \omega_n^k A_2(\omega_{\frac{n}{2}}^k) \end{aligned} \end{cases} \]
Substituting into the original sum:
\[ \sum_{k = 0}^{\frac{n}{2} - 1} A_1(\omega_{\frac{n}{2}}^k) + \omega_n^k A_2(\omega_{\frac{n}{2}}^k) , \sum_{k = 0}^{\frac{n}{2} - 1} A_1(\omega_{\frac{n}{2}}^k) - \omega_n^k A_2(\omega_{\frac{n}{2}}^k) \]
We can see that in the end the whole computation depends only on the interval \([0, \frac{n}{2})\), so the problem size has been cut in half. The time complexity ends up being \(O(n \log n)\); this algorithm is also called the Cooley-Tukey algorithm.
Deriving the inverse Fast Fourier Transform
We have now finished the Fast Fourier Transform. Suppose we have already handled the point-value multiplication; then what we need to do now is convert the point-value representation back into the coefficient representation.
We have now finished computing the point values, so what we have now is the polynomial in point-value form \((y_0, y_1, y_2, \dots)\). Now consider cooking up a new polynomial \(B(x) = \sum_{i = 0}^{n - 1} y_i x^i\). We now substitute in a set of complex numbers conjugate to the ones above and get the point-value representation of \(B(x)\):
\[ \begin{aligned} c_k &= B(\omega_n^{-k}) = \sum_{i = 0}^{n - 1} y_i (\omega_n^{-k})^i \\ &= \sum_{i = 0}^{n - 1} (\sum_{j = 0}^{n - 1} a_j (\omega_n^i)^j )(\omega_n^{-ki}) \\ &= \sum_{i = 0}^{n - 1} \sum_{j = 0}^{n - 1} a_j \omega_n^{i(j - k)} \\ &= \sum_{i = 0}^{n - 1} \sum_{j = 0}^{n - 1} a_j (\omega_n^{j-k})^i \\ &= \sum_{j = 1}^{n - 1} a_j \sum_{i = 0}^{n - 1} (\omega_n^{j - k})^i \end{aligned} \]
We find that the trailing \(\sum_{i = 0}^{n - 1} (\omega_n^{j - k})^i\) is the geometric series sum from high-school math (thanks here to Teacher Wang for the teaching), and consider writing it as:
\[ S(\omega_n^k) = 1 + \omega_n^k + \omega_n^{2k} + \dots + \omega_n^{(n - 1)k} \]
In the case \(n = k\), obviously \(S(\omega_n^k) = n\). Consider the case \(n \neq k\):
\[ S(\omega_n^k) = \frac{1 - \omega_n^{nk}}{1 - \omega_n^k} = 0 \]
Just substitute it in:
\[ \begin{aligned} c_k &= \sum_{j = 1}^{n - 1} a_j \sum_{i = 0}^{n - 1} (\omega_n^{j - k})^i \\ &= \sum_{j = 1}^{n - 1} a_j S(\omega_n^{j - k}) = na_k \end{aligned} \]
We find that \(a_k = \frac{c_k}{n}\). So, the places where the inverse transform has to differ from the transform are:
- Have the conjugate complex numbers take part in the computation.
- After computing, remember to divide by \(n\).
Implementation
Bit reversal
Consider the moment the enumeration reaches the boundary:
000 001 010 011 100 101 110 111
0 1 2 3 4 5 6 7
0 2 4 6 - 1 3 5 7
0 4 - 2 6 - 1 5 - 3 7
0 - 4 - 2 - 6 - 1 - 5 - 3 - 7
000 100 010 110 001 101 011 111
// src: https://oi.men.ci/fft-notes/
The butterfly operation is just opening one more variable for temporary storage, that's it.
Code
// LOJ108.cpp
#include <bits/stdc++.h>
using namespace std;
typedef complex<double> cd;
const double Pi = acos(-1.0);
const int MAX_N = 3e5 + 2000;
cd A[MAX_N], B[MAX_N];
int n, m, rev[MAX_N], max_bit, max_power;
void fft_initialize()
{
for (max_bit = 1, max_power = 2; (1 << max_bit) < n + m - 1; max_bit++)
max_power <<= 1;
for (int i = 0; i < max_power; i++)
rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (max_bit - 1)));
}
void fft(cd *arr, int len, int dft)
{
for (int i = 0; i < len; i++)
if (i < rev[i])
swap(arr[i], arr[rev[i]]);
for (int step = 1; step < len; step <<= 1)
{
cd omega_n = exp(cd(0, dft * Pi / step));
for (int j = 0; j < len; j += (step << 1))
{
cd omega_nk(1, 0);
for (int k = j; k < j + step; k++, omega_nk *= omega_n)
{
cd t = omega_nk * arr[k + step];
arr[k + step] = arr[k] - t;
arr[k] += t;
}
}
}
if (dft == -1)
for (int i = 0; i < len; i++)
arr[i] /= len;
}
int main()
{
scanf("%d%d", &n, &m), n++, m++, fft_initialize();
for (int i = 0, tmp; i < n; i++)
scanf("%d", &tmp), A[i].real(tmp);
for (int i = 0, tmp; i < m; i++)
scanf("%d", &tmp), B[i].real(tmp);
fft(A, max_power, 1), fft(B, max_power, 1);
for (int i = 0; i < max_power; i++)
A[i] *= B[i];
fft(A, max_power, -1);
for (int i = 0; i < n + m - 1; i++)
printf("%d ", (int)(A[i].real() + 0.5));
return 0;
}