PrevNext
Rare
 0/10

Matrix Exponentiation

Authors: Benjamin Qi, Harshini Rayasam, Neo Wang, Peng Bai

Repeatedly multiplying a square matrix by itself.

Edit This Page
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:

F0=0F1=1Fn+1=Fn+Fn1\begin{align*} F_0 &= 0 \\ F_1 &= 1 \\ F_{n+1} &= F_{n} + F_{n-1} \end{align*}

We can also write them using the following matrix recurrence:

A=[1110]n=[Fn+1FnFnFn1]A = \begin{bmatrix}1&1\\1&0\end{bmatrix}^n=\begin{bmatrix}F_{n+1} & F_n \\ F_n & F_{n-1}\end{bmatrix}

Proof by Induction

Base case (n=1n=1):

A1=[1110]=[F2F1F1F0]A^1 = \begin{bmatrix}1 & 1 \\ 1 & 0\end{bmatrix} = \begin{bmatrix}F_2 & F_1 \\ F_1 & F_0\end{bmatrix}

Inductive step (n+1n+1):

An+1=AAn=[1110][Fn+1FnFnFn1]=[Fn+1+FnFn+Fn1Fn+1Fn]=[Fn+2Fn+1Fn+1Fn]A^{n+1}=A\,A^n=\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} F_{n+1} & F_{n} \\ F_{n} & F_{n-1} \end{bmatrix}=\begin{bmatrix} F_{n+1}+F_n & F_{n}+F_{n-1} \\ F_{n+1} & F_{n} \end{bmatrix}=\begin{bmatrix} F_{n+2} & F_{n+1} \\ F_{n+1} & F_{n} \end{bmatrix}

With this, we've shown that the formula holds for all nNn \in \mathbb{N}.

Implementation

Time Complexity: O(logN)\mathcal{O}(\log N)

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 List
MOD = 10**9 + 7
def 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 MM. We know that following equation must hold:

[M1,1M1,2Mk,1Mk,k][a1ak]=[a2ak+1]\begin{bmatrix} M_{1,1} & M_{1, 2} & \dots \\ \vdots & \ddots & \vdots \\ M_{k, 1} & \dots & M_{k, k} \end{bmatrix} \begin{bmatrix} a_1 \\ \vdots \\ a_k \end{bmatrix} = \begin{bmatrix} a_2 \\ \vdots \\ a_{k + 1} \end{bmatrix}

Here, we use the values of a1,,ana_1, \dots, a_n to find ak+1a_{k+1}. We can also discard a1a_1 since it won't be used to calculate ak+2a_{k+2}. If we thinking about the matrix multiplication, we notice that there is a diagonal of 11s shifted to the right by 11 since aiai+1a_i \rarr a_{i + 1} for i<ki < k. So now we have (using k=5k = 5 as an example):

M=[01000001000001000001M5,1M5,2M5,3M5,4M5,5]M = \begin{bmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ M_{5, 1} & M_{5, 2} & M_{5, 3} & M_{5, 4} & M_{5, 5} \end{bmatrix}

To find the bottom row, we use the recurrence formula:

M=[01000001000001000001c5c4c3c2c1]M = \begin{bmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ c_5 & c_4 & c_3 & c_2 & c_1 \end{bmatrix}

With this MM, the equation always holds.

Implementation

Time Complexity: O(K3logN)\mathcal{O}(K^3 \log N)

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 MM, then deducing MM 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 k=5k = 5 as an example) to solve this modified recurrence for i>ki > k:

ai=c1ai1+c2ai2+...+ckaik+Ca_i = c_1a_{i-1} + c_2a_{i-2} + ... + c_ka_{i-k} + C

where CC is a given constant.

Solution

Problems

StatusSourceProblem NameDifficultyTags
CSESEasy
Show TagsExponentiation, Matrix
CSESEasy
Show TagsExponentiation, Matrix
CFEasy
Show TagsExponentiation, Matrix
CFNormal
Show TagsExponentiation, Matrix, PURS
Baltic OINormal
Show TagsExponentiation, Matrix
Balkan OINormal
Show TagsExponentiation, Matrix
DMOJNormal
Show TagsExponentiation, Matrix
PlatinumHard
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!

PrevNext