Rare
0/9
Matrix Exponentiation
Authors: Benjamin Qi, Harshini Rayasam, Neo Wang
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 by the following matrix relation
Proof by Induction
The fibonacci numbers are defined as follows:
Base case ():
Induction step ():
The base case is true, and the induction step is true. Therefore the formula holds for all .
C++
Implementation
Time complexity:
#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):
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 | |||
Plat | 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!