-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTDL016.cpp
151 lines (135 loc) · 3.82 KB
/
CTDL016.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
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;
for (int i = 0; i < A.size(); i++) {
trace = (trace + X[i][i]) % MOD;
}
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;
}
}
*/
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
vector<vector<long long>> nhan2vector (vector<vector<long long>> x, vector<vector<long long>> y, long long n){ // hàm tính tích 2 ma trận
vector<vector<long long>> ans(n, vector<long long>(n));
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
for (int p = 0; p < n; p++){
ans[i][j] = (ans[i][j] + x[i][p] * y[p][j]) % mod;
}
}
}
return ans;
}
//
vector<vector<long long>> luythua (vector<vector<long long>> matrix, long long n, long long k){
vector<vector<long long>> ans(n, vector<long long>(n));
if (k == 1){
return matrix;
}
ans = luythua(matrix, n, k / 2);
ans = nhan2vector(ans, ans, n);
if (k % 2){
ans = nhan2vector(ans, matrix, n);
}
return ans;
}
/* // vẫn đúng nhưng quá lâu
vector<vector<long long>> luythua (vector<vector<long long>> matrix, long long n, long long k){
vector<vector<long long>> ans(n, vector<long long>(n));
if (k == 1){
return matrix;
}
if (k % 2 == 0){
return ans = nhan2vector(luythua(matrix, n, k / 2), luythua(matrix, n, k/2), n);
}else{
return ans = nhan2vector(nhan2vector(luythua(matrix, n, k / 2), luythua(matrix, n, k/2), n), matrix, n);
}
return ans;
}
*/
/* nhanh tương tự cách đầu tiên
vector<vector<long long>> luythua (vector<vector<long long>> matrix, long long n, long long k){
vector<vector<long long>> ans(n, vector<long long>(n));
ans = nhan2vector(matrix, matrix, n);
k -= 2;
while(k > 0){
if (k % 2 == 1){
ans = nhan2vector(ans, matrix, n);
}
matrix = nhan2vector(matrix, matrix, n);
k /= 2;
}
return ans;
}
*/
void count(vector<vector<long long>> matrix, long long n, long long k){ // in kết quả tổng của đường chéo chính.
vector<vector<long long>> ans = luythua(matrix, n, k);
long long sum = 0;
for (int i = 0; i < n; i++){
sum = (sum + ans[i][i]) % mod;
}
cout<<sum;
}
int main(){
int t; cin >> t;
while(t--){
long long n, k;
cin >> n >> k;
vector<vector<long long>> matrix(n, vector<long long>(n)); // tạo ma trận 2 chiều với n hàng, mỗi hàng là 1 vector kích thước n
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> matrix[i][j];
}
}
count(matrix, n, k);
cout<<endl;
}
}