1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstdio> 7 #include<cstring> 8 #include<list> 9 10 using namespace std; 11 12 #define maxn 15 13 int n, k; 14 struct matrix//定义一个结构体,方便传递值 15 { 16 int m[maxn][maxn]; 17 }; 18 19 /* 20 maxn和mod由全局定义,其中mod根据需要可以省去 21 */ 22 23 matrix mul(matrix a, matrix b) //矩阵求积, 矩阵乘法 24 { 25 matrix ans; 26 for(int i = 1; i <= n; i++) 27 { 28 for(int j = 1; j <= n; j++) 29 { 30 ans.m[i][j] = 0; 31 for(int k = 1; k <= n; k++) 32 { 33 ans.m[i][j] += (a.m[i][k] * b.m[k][j]) % 9973; 34 ans.m[i][j] %= 9973; 35 } 36 } 37 } 38 return ans; 39 } 40 41 matrix quick_pow(matrix a, int b) //矩阵快速幂 42 { 43 matrix ans; 44 for(int i = 1; i <= n; i++) 45 { 46 for(int j = 1; j <= n; j++) 47 { 48 if(i == j) 49 ans.m[i][j] = 1; 50 else 51 ans.m[i][j] = 0;//这里要初始化为单位矩阵,类比普通快速幂这里初始化为1 52 } 53 } 54 while(b != 0)//方法与普通快速幂相同,只有乘法的实现不同 55 { 56 if(b % 2 == 1) 57 ans = mul(a, ans); 58 a = mul(a, a); 59 b /= 2; 60 } 61 62 return ans; 63 } 64 65 66 int main() 67 { 68 int T; 69 cin >> T; 70 while(T--) 71 { 72 matrix a; 73 74 cin >> n >> k; 75 for(int i = 1; i<= n; ++i) 76 for(int j = 1; j <= n; ++j) 77 cin >> a.m[i][j]; 78 79 matrix tmp = quick_pow(a, k); 80 int ans = 0; 81 for(int i = 1; i<= n; ++i) 82 ans += tmp.m[i][i] % 9973; 83 84 ans %= 9973; // 最后这里一定要再次取余! 85 cout << ans << endl; 86 } 87 88 89 return 0; 90 }