0%

P3390 【模板】矩阵快速幂

思路讲解

1
2
3
4
5
6
7
8
9
10
11
12
13
// 矩阵快速幂:必须是方阵
template <class T>
Matrix<T> mat_pow(Matrix<T> base, long long exp) {
assert(base.n == base.m);
Matrix<T> res = Matrix<T>::identity(base.n);
while (exp > 0) {
if (exp & 1) res = res * base;
base = base * base;
exp >>= 1;
}
return res;
}

AC代码

心路历程(WA,TLE,MLE……)