Matrix Exponentiation
Turn a linear recurrence or a walk-counting question into one matrix, then raise it to a huge power in O(d^3 log n) with repeated squaring.
The problem
Fibonacci numbers are defined by . Print for up to .
Other questions with the same shape:
- A recurrence like with a huge .
- A directed graph with 50 vertices: in how many ways can you walk exactly edges from vertex 1 to vertex 50?
1 of 9