【学习笔记】〖九度OJ〗题目1443:Tr A
间。
#include<iostream> using namespace std; #define MOD 9973 class Matrix { public: int m[11][11]; int n;//方阵的行数 int tr; Matrix() { tr = 0; for (int i=0; i<11; i++) { for (int j=0; j<11; j++) { m[i][j] = 0; } } } Matrix(int n) { tr = 0; this->n = n; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { m[i][j] = 0; } } } void getT(int n) { tr=0; for (int i=0; i<n; i++) { tr = (tr + m[i][i])%MOD; } } Matrix operator * (const Matrix a) const { Matrix res; res.n = n; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { res.m[i][j] = 0; for (int k=0; k<n; k++) { res.m[i][j] = (res.m[i][j] + (m[i][k] * a.m[k][j]) % MOD) % MOD; } } } return res; } }; int main() { freopen("input.in", "r", stdin); freopen("output.out","w", stdout); int num; while (cin >> num && num!=0) { int n, k, i, j, x, y, z; for (i=0; i<num; i++)//每组一次 { //输入部分 cin >> n >> k; Matrix mtx(n); for (x=0; x<n; x++) { for (y=0; y<n; y++) { cin >> mtx.m[x][y]; } } //计算部分 int tmp = k; Matrix res(n); for (z=0; z<n; z++) { res.m[z][z] = 1; } while (k>0) { if (k%2 == 1) { res = res * mtx; } mtx = mtx*mtx; k/=2; } res.getT(n); cout << res.tr << endl; }//end of for }//end of while return 0; }