Matrix Exponentiation
Authors: Benjamin Qi, Harshini Rayasam, Neo Wang, Peng Bai
Repeatedly multiplying a square matrix by itself.
Prerequisites
Resources | |||||
---|---|---|---|---|---|
CP2 | |||||
CPH | |||||
CF | video + problemset | ||||
CF | interesting applications of mat exp | ||||
Mostafa | powerpoint of matrix exponentiation |
Example - Fibonacci
Focus Problem – try your best to solve this problem before continuing!
The Fibonacci numbers are defined as follows:
We can also write them using the following matrix recurrence:
Proof by Induction
Base case ():
Inductive step ():
With this, we've shown that the formula holds for all .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;const ll MOD = 1e9 + 7;using Matrix = array<array<ll, 2>, 2>;Matrix mul(Matrix a, Matrix b) {
Python
from typing import ListMOD = 10**9 + 7def mul(A: List[List[int]], B: List[List[int]], MOD: int) -> List[List[int]]:C = [[0, 0], [0, 0]]for i in range(2):for j in range(2):for k in range(2):
Finding the Right Matrix - Generalized Linear Recurrences
Focus Problem – try your best to solve this problem before continuing!
We can generalize the technique above to calculate values in any linear recurrence. The tricky part is finding the correct matrix for the problem. Let's see how we can find this matrix . We know that following equation must hold:
Here, we use the values of to find . We can also discard since it won't be used to calculate . If we thinking about the matrix multiplication, we notice that there is a diagonal of s shifted to the right by since for . So now we have (using as an example):
To find the bottom row, we use the recurrence formula:
With this , the equation always holds.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int MOD = 1e9;template <typename T> void matmul(vector<vector<T>> &a, vector<vector<T>> b) {int n = a.size(), m = a[0].size(), p = b[0].size();assert(m == b.size());vector<vector<T>> c(n, vector<T>(p));
Takeaway
The takeaway here is not the exact matrix used in linear recurrences, but the method in deriving it. The process of thinking about a vector before and after applying , then deducing through logic, is a technique that generalizes far beyond standard linear recurrences. For example, see if you can find the equation needed and correct matrix (using as an example) to solve this modified recurrence for :
where is a given constant.
Solution
Problems
Status | Source | Problem Name | Difficulty | Tags | ||
---|---|---|---|---|---|---|
CSES | Easy | Show TagsExponentiation, Matrix | ||||
CSES | Easy | Show TagsExponentiation, Matrix | ||||
CF | Easy | Show TagsExponentiation, Matrix | ||||
CF | Normal | Show TagsExponentiation, Matrix, PURS | ||||
Baltic OI | Normal | Show TagsExponentiation, Matrix | ||||
Balkan OI | Normal | Show TagsExponentiation, Matrix | ||||
DMOJ | Normal | Show TagsExponentiation, Matrix | ||||
Platinum | Hard | Show TagsExponentiation, Matrix |
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!