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 : two tokens of weight zero, one of weight one, and three of weight two. Blue tokens have . How many ordered red-blue pairs have each total weight?
Write and . The answer is
The coefficient of collects exactly the pairs whose weights sum to . 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.
Evaluation turns multiplication into independent work
An -term polynomial is determined by its values at 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 and a primitive -th root . Then
Why does inversion work? The inner geometric sum is when its exponent is divisible by , and zero otherwise. No floating-point approximation appears.
For , primitive root supplies every power-of-two length up to . At a butterfly of width , combine values as and . There are stages, each with work.
Padding is a mathematical requirement
An -point transform multiplies modulo . Without enough zeros, a term becomes , contaminating low coefficients. Set to the smallest supported power of two with .
For our example, length two is disastrous: the term wraps into the coefficient of . Length four is sufficient. Padding is therefore not merely an implementation convention.
One prime does not recover arbitrary integers
An NTT returns coefficients modulo . To recover nonnegative integer coefficients, first prove an upper bound . Use several pairwise coprime transform primes whose product exceeds , 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 cannot generally be recovered from one transform prime. Reduction modulo has already discarded information. Multiple primes plus a sufficient bound, or a carefully justified splitting method, is required. Never replace the output modulus with at the final line and assume it is valid.
From convolution to correlation
For , reverse before multiplying. If has length , the desired sum is coefficient of . 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 . 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 , 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!




























































Conversation
Comments powered by Waline