Video failed to load

Bostan-Mori: The Trillionth Term Without a Trillion States

463 words
2 minutes
Bostan-Mori: The Trillionth Term Without a Trillion States

When a recurrence has order several thousand and the requested index is 101810^{18}, matrix exponentiation is usually the wrong abstraction. The recurrence has a compact rational generating function. Bostan-Mori repeatedly halves the requested index while keeping the polynomial degree bounded.

Turn the recurrence into a fraction#

Let an=c1an−1+⋯+cdan−da_n=c_1a_{n-1}+\cdots+c_da_{n-d} for n≥dn\ge d, with initial terms a0,…,ad−1a_0,\ldots,a_{d-1}. Set

Q(x)=1−c1x−⋯−cdxd,Q(x)=1-c_1x-\cdots-c_dx^d,

and take P=(Q⋅∑i=0d−1aixi) mod xdP=(Q\cdot\sum_{i=0}^{d-1}a_ix^i)\bmod x^d. Multiplying the full generating function by QQ cancels every coefficient from degree dd onward. Consequently A=P/QA=P/Q.

For the running example a0=2a_0=2, a1=3a_1=3, an=2an−1+an−2a_n=2a_{n-1}+a_{n-2}, we get P=2−xP=2-x, Q=1−2x−x2Q=1-2x-x^2. The sequence starts 2,3,8,19,462,3,8,19,46. Our task is coefficient extraction, not state simulation.

Make the denominator even#

Multiply numerator and denominator by Q(−x)Q(-x):

P(x)Q(x)=P(x)Q(−x)Q(x)Q(−x).\frac{P(x)}{Q(x)}= \frac{P(x)Q(-x)}{Q(x)Q(-x)}.

The denominator is unchanged under x↦−xx\mapsto-x, so its odd coefficients vanish. Write it as V(x2)V(x^2). Split the new numerator into even and odd parts:

P(x)Q(−x)=U0(x2)+xU1(x2).P(x)Q(-x)=U_0(x^2)+xU_1(x^2).

For an even requested index 2k2k, keep U0/VU_0/V and ask for coefficient kk. For an odd index 2k+12k+1, keep U1/VU_1/V instead. This is an exact identity; it does not discard contributions that could matter later.

Yes

No

No

Yes

Want coefficient N of P over Q

Multiply both by Q of minus x

Is N even?

Keep even numerator coefficients

Keep odd numerator coefficients

Keep even denominator coefficients

Replace N with floor N divided by 2

N equals zero?

Return P constant divided by Q constant

Yes

No

No

Yes

Want coefficient N of P over Q

Multiply both by Q of minus x

Is N even?

Keep even numerator coefficients

Keep odd numerator coefficients

Keep even denominator coefficients

Replace N with floor N divided by 2

N equals zero?

Return P constant divided by Q constant

Trace one iteration#

For our fraction,

P(x)Q(−x)=2+3x−4x2+x3,P(x)Q(-x)=2+3x-4x^2+x^3,Q(x)Q(−x)=1−6x2+x4.Q(x)Q(-x)=1-6x^2+x^4.

To find a4a_4, take the even numerator, yielding (2−4x)/(1−6x+x2)(2-4x)/(1-6x+x^2), now at index two. One more even extraction gives (2−22x)/(1−34x+x2)(2-22x)/(1-34x+x^2) at index one. Selecting the odd numerator on the next iteration gives constant 46. No intermediate prefix of length four was required by the algorithm.

Why degrees stay under control#

If deg⁡P<d\deg P<d and deg⁡Q=d\deg Q=d, multiplication raises degrees to at most 2d−12d-1 and 2d2d. Extracting one parity divides them roughly in half, restoring the original bound. Each step costs O(M(d))O(M(d)), and there are O(log⁡N)O(\log N) steps. With NTT, the total is O(dlog⁡dlog⁡N)O(d\log d\log N) and working storage is linear in dd for the straightforward version.

The companion implementation exposes wsh::bostan_mori(P, Q, N). It accepts an unsigned 64-bit index, handles a zero numerator, and requires an invertible constant of QQ. Coefficients must use the header’s modulus.

Choosing the method#

For order two or three, tiny matrix exponentiation can be simpler and faster. For a single huge-index query with a large order, Bostan-Mori avoids dense matrices. If you need all coefficients through NN and NN is moderate, compute P⋅Q−1P\cdot Q^{-1} instead. If the recurrence is unknown, discovering it is a separate problem; Bostan-Mori does not infer a recurrence from arbitrary data.

Exercise. Let bnb_n count ordered constructions from pieces of sizes two and five, with b0=1b_0=1. Derive P,QP,Q and find b10b_{10} by hand before attempting b1018b_{10^{18}} modulo 998244353998244353. Explain why a multiset interpretation would produce a different denominator.

Support & Share

If this article helped you, please share or support!

Sponsor
Bostan-Mori: The Trillionth Term Without a Trillion States
https://blog.wsh.moe/posts/bostan-mori/
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