struct Matrix {
i64 N;
vector<vector<i64>> A;
Matrix() { N = 0;}
Matrix(int n) {
N = n;
A.resize(N + 1);
for (int i = 0; i <= N; i ++)
A[i].resize(N + 1, 0);
}
void init(vector<vector<i64>> a, int n) {
A = a;
N = n;
}
Matrix operator*(const Matrix &b) const {
Matrix res(b.N);
for (int i = 1; i <= b.N; ++i)
for (int j = 1; j <= b.N; ++j)
for (int k = 1; k <= b.N; ++k)
res.A[i][j] = (res.A[i][j] + A[i][k] * b.A[k][j]);
return res;
}
Matrix qpow(i64 k) {
Matrix res(N);
//斐波那契数列初始化
//res.A[1][1] = res.A[1][2] = 1;
//A[1][1] = A[1][2] = A[2][1] = 1;
//单位矩阵
for (int i = 0; i <= N; i ++)
res.A[i][i] = 1;
while (k) {
if (k & 1) res = res * (*this);
(*this) = (*this) * (*this);
k >>= 1;
}
return res;
}
//求行列式
i64 det() {
return DET(A, N);
}
i64 DET(vector<vector<i64>> arr1, int n) {
i64 sum = 0;
//i是第一行的列指标,M是余子式的值,sum是行列式的计算值
if (n == 1)//一阶行列式直接得出结果
return arr1[0][0];
else if (n > 1) {
for (int i = 0; i < n; i++) {
//按照第一行展开
i64 M = Minor(arr1, i, n);
sum += pow(-1, i + 2) * arr1[0][i] * M;
}
}
return sum;
}
i64 Minor(vector<vector<i64>> arr1, int i, int n) {
vector arr2(n + 1, vector<i64>(n + 1));
//以下为构造余子式的过程。
for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < n - 1; k++) {
if (k < i)
arr2[j][k] = arr1[j + 1][k];
else if (k >= i)
arr2[j][k] = arr1[j + 1][k + 1];
}
}
return DET(arr2, n - 1);
//构造完后,余子式是一个新的行列式,返回DET函数进行计算。
}
};