#include<cstring>
#define ll long long
#define N
#define mod
struct Mat {
ll mat[N][N];
};
Mat operator * (Mat a, Mat b) {//N阶方阵
Mat c;
memset(c.mat, 0, sizeof(c.mat));
for(int k = 0; k < N; ++k) {
for(int i = 0; i < N; ++i) {
if(a.mat[i][k]==0) continue;//根据具体情况剪枝
for(int j = 0; j < N; ++j) {
if(b.mat[k][j]==0) continue;//根据具体情况剪枝
c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
c.mat[i][j]%=mod;//根据具体情况防止溢出
}
}
}
return c;
}
Mat operator ^ (Mat a, int k) {
Mat c;
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
c.mat[i][j] = (i == j); //初始化为单位矩阵
for(; k; k >>= 1) {
if(k&1) c = c*a;
a = a*a;
}
return c;
}
矩阵快速幂模板
最新推荐文章于 2022-08-27 21:03:02 发布