Skip to content

Commit

Permalink
✨ adds matrix exponentiation isolated function
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Jan 13, 2024
1 parent e1dfb0a commit 33461cd
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions algorithms/math/matrix-exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ll MOD = 1'000'000'007;

template <typename T>
vector<vector<T>> prod(vector<vector<T>> &a,
vector<vector<T>> &b) {
int n = len(a);
vector<vector<T>> c(n, vector<T>(n));

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
c[i][j] =
(c[i][j] + ((a[i][k] * b[k][j]) % MOD)) % MOD;
}
}
}

return c;
}

template <typename T>
vector<vector<T>> fpow(vector<vector<T>> &xs, ll p) {
vector<vector<T>> ans(len(xs), vector<T>(len(xs)));
for (int i = 0; i < len(xs); i++) ans[i][i] = 1;

auto b = xs;
while (p) {
if (p & 1) ans = prod(ans, b);
p >>= 1;
b = prod(b, b);
}
return ans;
}

0 comments on commit 33461cd

Please sign in to comment.