-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTDL014.cpp
68 lines (60 loc) · 1.42 KB
/
CTDL014.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
using namespace std;
typedef vector<vector<long long>> matrix;
matrix multiply(matrix A, matrix B) {
int N = A.size();
matrix result(N, vector<long long>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
result[i][j] = (result[i][j] + A[i][k] * B[k][j]) % MOD;
}
}
}
return result;
}
matrix power(matrix A, int K) {
int N = A.size();
if (K == 0) {
matrix identity(N, vector<long long>(N));
for (int i = 0; i < N; i++) {
identity[i][i] = 1;
}
return identity;
}
if (K == 1) {
return A;
}
matrix B = power(A, K / 2);
B = multiply(B, B);
if (K % 2) {
B = multiply(B, A);
}
return B;
}
long long solve(matrix A, int K) {
matrix X = power(A, K);
long long trace = 0;
int j = A.size() - 1;
for (int i = 0; i < A.size(); i++) {
trace = (trace + X[i][j]) % MOD;
j--;
}
return trace;
}
int main(){
int t; cin >> t;
while(t--){
long long n, k;
cin >> n >> k;
vector<vector<long long>> A(n, vector<long long>(n));
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> A[i][j];
}
}
cout<<solve(A, k)<<endl;
}
}