From Matrix Exponentiation to Kitamasa Method
--- Hi, I'm AmirMohammad (commonly known as Danet). Naturally, as a non-native speaker, the translation and editing process involved some assistance. If you spot any technical or linguistic errors, I would be grateful if you pointed them out!
Faster Computation of Linear Recurrence DP
Problem Statement
We are given a linear recurrence relation of order ( k ):
Along with the first ( k ) values:
Our goal is to compute ( dp[n] ) efficiently for very large ( n ).
Classical Solution: Matrix Exponentiation
State Definition
To compute the recurrence, we always need the last ( k ) values. So we define the state vector as:
This vector fully describes the DP state at step ( i ).
Transition Rule There exists a fixed matrix ( M ) such that:
Applying this transition repeatedly:
So the problem reduces to exponentiating a matrix.
Transition Matrix The transition matrix has the following form:
- The first row applies the recurrence
- The remaining rows only shift values downward
Time Complexity
This is correct but becomes too slow when ( k ) is large.
Key Observation
We do not actually need the entire matrix ( M^n ).
Important facts: - Only the first row affects ( dp[n] ) - All other rows are pure shifts - The transition itself is linear and fixed
This suggests that the problem can be solved without matrices.
Shift Operator Interpretation Introduce a symbolic operator ( x ) that represents one DP transition.
Interpretation:
So: - ( x ) behaves exactly like applying the recurrence once - ( x^n ) means applying the recurrence ( n ) times
This operator plays the same role as the matrix ( M ), but algebraically.
Characteristic Polynomial From the recurrence:
Replace ( dp[i] ) with powers of ( x ):
Move everything to the left-hand side:
Define the characteristic polynomial:
This polynomial encodes the recurrence completely.
Main Idea of Kitamasa Method
We want to compute ( x^n ).
Using polynomial division:
Taking modulo ( p(x) ):
Where:
So the remainder can be written as:
Mapping this back to DP:
Conclusion:
Computing ( dp[n] ) is equivalent to computing
( x^n \bmod p(x) ).
Fibonacci Example
Consider Fibonacci numbers:
Here ( k = 2 ), and the characteristic polynomial is:
Which implies:
Computing f(3):
Therefore:
Computing f(4):
Thus:
---
Binary Exponentiation on Polynomials
Assume we already know:
To compute (x^(2n)):
Steps: 1. Multiply two polynomials of degree ( < k ) 2. Reduce the result modulo p(x).
Each step costs O(k^2).
Final Complexity
Using FFT:








Valuable Blog — Upvoted :)
Auto comment: topic has been updated by Siyah (previous revision, new revision, compare).
Auto comment: topic has been updated by Siyah (previous revision, new revision, compare).
Auto comment: topic has been updated by Siyah (previous revision, new revision, compare).
can someone add some problems related to this?
Not exactly related (although, you can solve it like this) but this AtCoder G that used the BM algorithm is essentially the same idea as in this blog: https://atcoder.jp/contests/abc436/tasks/abc436_g
This was actually covered in my linear algebra course. It's a result of the Cayley-Hamilton theorem, which states that any square matrix over a commutative ring satisfies its characteristic equation, i.e. for a matrix $$$A$$$, if you take the polynomial $$$p(X)=\mathrm{det}(A-XI_n)$$$, then $$$p(A)=0$$$. It can be shown that this polynomial is the same as the one given in the blog. From there you can get $$$A^n=Q(A)p(A)+R(A)=R(A)$$$
I don't think you need Cayley-Hamilton here, I think saying that you represent the number of f(n)'s with coefficient of x^n is enough, and then it just works
upvote for you!!
Cool blog. Could you explain how you do the remainder part faster than $$$O(k^2)$$$ to get the FFT complexity so low? I don't know/see how that is done.
Check this
Thanks a lot
Coool!
I just want to mention that there is another way to do fast linear recurrence evaluation. The $$$O(k \log k \log n)$$$ presented here is what you find in most tutorials and for a long time it was the only way which I knew. In my opinion, this is not suited for ICPC contest references since it requires fast "polynomial modulo polynomial". This other method only requires standard fft polynomial multiplication and if you only need $$$O(k^2 \log n)$$$ you can also replace the fft by a quadratic multiplication. You can read about the idea in this blog this blog in the section "Graeffe's method".