Video failed to load

Exact Convolution: NTT, Aliasing, and the Modulus Trap

623 words
3 minutes
Exact Convolution: NTT, Aliasing, and the Modulus Trap
Exact Convolution: NTT, Aliasing, and the Modulus Trap

Fast convolution is useful long after you learn polynomial multiplication. In difficult problems, the real work is discovering that a transition, a difference distribution, or a collection of pair counts is a coefficient of a product. This article assumes modular arithmetic and divide and conquer. It is advanced training material, not a claim about a particular contest rating.

Start with a counting question#

Suppose red tokens have weights described by A=[2,1,3]A=[2,1,3]: two tokens of weight zero, one of weight one, and three of weight two. Blue tokens have B=[1,4]B=[1,4]. How many ordered red-blue pairs have each total weight?

Write A(x)=2+x+3x2A(x)=2+x+3x^2 and B(x)=1+4xB(x)=1+4x. The answer is

C(x)=2+9x+7x2+12x3.C(x)=2+9x+7x^2+12x^3.

The coefficient of xkx^k collects exactly the pairs whose weights sum to kk. Move the coefficient selector below to inspect the individual contributions. This example uses ordinary small integers; the contest implementation performs the same operations modulo a prime.

Convolution example: A = [2, 1, 3], B = [1, 4], C = [2, 9, 7, 12]. Enable JavaScript to inspect each coefficient.

Evaluation turns multiplication into independent work#

An LL-term polynomial is determined by its values at LL distinct points. Evaluate both inputs at the same points, multiply the values, then interpolate. Arbitrary points do not immediately make this fast. Roots of unity do: the even and odd coefficients share a smaller evaluation problem.

Choose a prime pp and a primitive LL-th root ω\omega. Then

A^j=∑i=0L−1Aiωij,Ai=L−1∑j=0L−1A^jω−ij.\widehat A_j=\sum_{i=0}^{L-1} A_i\omega^{ij},\qquad A_i=L^{-1}\sum_{j=0}^{L-1}\widehat A_j\omega^{-ij}.

Why does inversion work? The inner geometric sum is LL when its exponent is divisible by LL, and zero otherwise. No floating-point approximation appears.

For p=998244353=119⋅223+1p=998244353=119\cdot2^{23}+1, primitive root 33 supplies every power-of-two length up to 2232^{23}. At a butterfly of width 2h2h, combine values u,vu,v as u+wvu+w v and u−wvu-w v. There are log⁡2L\log_2 L stages, each with O(L)O(L) work.

Coefficient arrays

Pad to length L

Forward NTT on both arrays

Multiply matching values

Inverse NTT

Keep n plus m minus 1 coefficients

Coefficient arrays

Pad to length L

Forward NTT on both arrays

Multiply matching values

Inverse NTT

Keep n plus m minus 1 coefficients

Padding is a mathematical requirement#

An LL-point transform multiplies modulo xL−1x^L-1. Without enough zeros, a term xL+tx^{L+t} becomes xtx^t, contaminating low coefficients. Set LL to the smallest supported power of two with L≥n+m−1L\ge n+m-1.

For our example, length two is disastrous: the 12x312x^3 term wraps into the coefficient of xx. Length four is sufficient. Padding is therefore not merely an implementation convention.

One prime does not recover arbitrary integers#

An NTT returns coefficients modulo pp. To recover nonnegative integer coefficients, first prove an upper bound HH. Use several pairwise coprime transform primes whose product exceeds HH, then apply the Chinese remainder theorem. Signed recovery needs a product exceeding twice the absolute coefficient bound and a centered representative.

Likewise, convolution modulo an unrelated modulus MM cannot generally be recovered from one transform prime. Reduction modulo pp has already discarded information. Multiple primes plus a sufficient bound, or a carefully justified splitting method, is required. Never replace the output modulus with MM at the final line and assume it is valid.

From convolution to correlation#

For Dk=∑iaibi+kD_k=\sum_i a_i b_{i+k}, reverse aa before multiplying. If aa has length nn, the desired sum is coefficient n−1+kn-1+k of rev⁡(A)B\operatorname{rev}(A)B. Write out this index once on paper; guessing the reversal is a reliable way to lose a hard problem.

Implementation and practice#

The C++17 companion header provides wsh::multiply and checks transform length. Inputs must be normalized into [0,p)[0,p). Its largest products fit signed 64-bit integers. Transform lengths are constrained by the prime even if the machine has more memory.

Exercise. Given two multisets of signed integers in [−W,W][-W,W], count pairs with each possible difference. Offset frequencies to nonnegative indices, reverse one distribution, and derive the exact output index. Then explain which coefficient bound is necessary if the answer must be an exact 64-bit count.

Continue with Newton inversion and CDQ convolution DP, where multiplication becomes a building block rather than the final answer.

Support & Share

If this article helped you, please share or support!

Sponsor
Exact Convolution: NTT, Aliasing, and the Modulus Trap
https://blog.wsh.moe/posts/ntt-exact-convolution/
Author
Wsh
Published at
2026-10-24

Comments

LEAVE A NOTE

Join the conversation

Markdown supported

Be kind. Good conversations start with you.

Conversation

Loading conversation…

Comments powered by Waline

Profile Image of the Author
Wsh
Nice to meet you! Hello, I'm Wsh.
Announcement
Any news or updates will be shared here, so check back anytime! Thanks for stopping by, meow >.<
Categories
Tags
Site Statistics
Posts
27
Categories
7
Tags
69
Total Words
124,615
Running Days
0 days
Last Activity
0 days ago
Site Info
Build Platform
Vercel
Blog Version
Firefly v6.16.8
License
CC BY-NC-SA 4.0
Table of Contents