struct Matrix {
static const int MAXN = 100;
static const int MOD = 1e9 + 7;
int n, a[MAXN + 5][MAXN + 5];
Matrix(int _n = 0) {n = _n;}
int add(const int &a, const int &b)const {
int c = a + b;
if(c >= MOD)
c -= MOD;
return c;
}
int sub(const int &a, const int &b)const {
int c = a - b;
if(c < 0)
c += MOD;
return c;
}
int mul(const int &a, const int &b)const {
ll c = 1ll * a * b;
if(c >= MOD)
c %= MOD;
return (int)c;
}
void Init(int _n, const int _a[MAXN + 5][MAXN + 5]) {
n = _n;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
a[i][j] = _a[i][j];
}
}
void ZERO(int _n) {
n = _n;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
a[i][j] = 0;
}
}
void ONE(int _n) {
n = _n;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
a[i][j] = (i == j);
}
}
Matrix operator+(const Matrix& m)const {
Matrix res(n);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
res.a[i][j] = add(a[i][j], m.a[i][j]);
}
return res;
}
Matrix operator-(const Matrix& m)const {
Matrix res(n);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
res.a[i][j] = sub(a[i][j], m.a[i][j]);
}
return res;
}
Matrix operator*(const Matrix& m)const {
Matrix res;
res.ZERO(n);
for(int k = 1; k <= n; ++k) {
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
res.a[i][j] = add(res.a[i][j], mul(a[i][k], m.a[k][j]));
}
}
return res;
}
Matrix pow(ll k)const {
Matrix x, res;
x.Init(n, a);
res.ONE(n);
while(k) {
if(k & 1)
res = res * x;
x = x * x;
k >>= 1;
}
return res;
}
void Show(string name = "") {
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
printf("%d%c", a[i][j], " \n"[j == n]);
}
}
};
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步