Video failed to load

Newton Lifting: Inverting a Million-Term Formal Power Series

501 words
3 minutes
Newton Lifting: Inverting a Million-Term Formal Power Series

Formal power series do not need to converge. In competitive programming, working modulo xnx^n means keeping precisely the first nn coefficients. Everything beyond that boundary is irrelevant. This finite viewpoint makes a seemingly infinite inverse computable.

The inverse exists exactly when the constant allows it#

Over a field, A(x)A(x) has a multiplicative inverse if and only if A0≠0A_0\ne0. The constant equation is A0B0=1A_0B_0=1. Every later coefficient is then determined by earlier ones:

Bk=−A0−1∑i=1kAiBk−i.B_k=-A_0^{-1}\sum_{i=1}^k A_iB_{k-i}.

This gives an O(n2)O(n^2) verifier. It is also the right fallback for tiny inputs. But evaluating the recurrence for a million terms is infeasible.

Take A=1−2x−x2A=1-2x-x^2. Its inverse begins

B=1+2x+5x2+12x3+29x4+⋯ .B=1+2x+5x^2+12x^3+29x^4+\cdots.

Check the coefficient of x3x^3: 12−2⋅5−2=012-2\cdot5-2=0. We will recover the same sequence with logarithmically many lifting stages.

Correct the error rather than solve every coefficient#

Assume AB≡1(modxm)AB\equiv1\pmod{x^m}. Define E=1−ABE=1-AB, so every nonzero term of EE has degree at least mm. Replace BB with

Bnew=B(2−AB)=B(1+E).B_{\text{new}}=B(2-AB)=B(1+E).

Multiplying by AA gives (1−E)(1+E)=1−E2(1-E)(1+E)=1-E^2. The new error starts at degree 2m2m. This algebraic identity is the whole precision-doubling proof. There is no analytic limit and no approximate convergence.

Starting at B=A0−1B=A_0^{-1}, repeatedly double the truncation boundary until it reaches nn.

Correct modulo x

Compute error through x squared

Multiply by one plus error

Correct modulo x squared

Repeat at twice the precision

Correct modulo x

Compute error through x squared

Multiply by one plus error

Correct modulo x squared

Repeat at twice the precision

For our polynomial, the first lift yields 1+2x1+2x modulo x2x^2. At the next lift, AB=1−5x2−2x3AB=1-5x^2-2x^3 modulo x4x^4. Thus B(2−AB)B(2-AB) gives 1+2x+5x2+12x31+2x+5x^2+12x^3 after truncation.

A safe implementation boundary#

The following is pseudocode. multiply returns an ordinary polynomial product; truncate both discards excess terms and pads missing coefficients with zero.

B = [inverse_mod(A[0])]
while length(B) < n:
target = min(2 * length(B), n)
E = truncate(multiply(A[0:target], B), target)
E = -E
E[0] += 2
B = truncate(multiply(B, E), target)
return B

The companion header implements this as wsh::inverse(A, n). It rejects a zero constant. All coefficient arithmetic is modulo 998244353998244353.

Do not use a cyclic convolution of length target without a separate derivation. Although we discard high-degree coefficients, they must not wrap into low-degree positions during multiplication. The straightforward implementation uses sufficiently padded ordinary products.

Complexity and storage#

If multiplication costs M(n)=O(nlog⁡n)M(n)=O(n\log n), the stages cost M(1)+M(2)+M(4)+⋯+M(n)=O(M(n))M(1)+M(2)+M(4)+\cdots+M(n)=O(M(n)). This geometric sum matters: charging every stage as a full size-nn transform would produce an unnecessary extra logarithm.

The simple implementation allocates a few linear-size arrays. It is suitable as a clear baseline; specialized NTT libraries reduce transforms and allocations. Always check the largest temporary product length, not just the requested final precision, against the transform prime’s limit.

Inversion unlocks division#

For polynomial division A=BQ+RA=BQ+R with deg⁡R<deg⁡B\deg R<\deg B, reverse the coefficient order. The leading coefficient of BB becomes the constant term of its reversal. If k=deg⁡A−deg⁡B+1k=\deg A-\deg B+1, then

rev⁡(Q)≡rev⁡(A) rev⁡(B)−1(modxk).\operatorname{rev}(Q)\equiv\operatorname{rev}(A)\, \operatorname{rev}(B)^{-1}\pmod{x^k}.

Here each reversal uses the polynomial’s own degree. Recover QQ, then subtract BQBQ to obtain RR. This is why fast inversion also appears in multipoint evaluation trees.

Exercise. Derive the inverse of 2+x2+x through degree four over the field modulo 17. Verify it by multiplication, then explain why attempting the same inversion over integers fails. The distinction between a field and an arbitrary coefficient ring is substantive.

Read logarithms and exponentials next.

Support & Share

If this article helped you, please share or support!

Sponsor
Newton Lifting: Inverting a Million-Term Formal Power Series
https://blog.wsh.moe/posts/fps-newton-inverse/
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