-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluy_thua_ma_tran1.cpp
More file actions
71 lines (68 loc) · 1.39 KB
/
luy_thua_ma_tran1.cpp
File metadata and controls
71 lines (68 loc) · 1.39 KB
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
long n, k;
struct matrix
{
ll a[10][10];
// Nap chong phep nhan matrix
friend matrix operator*(matrix x, matrix y)
{
matrix res;
// xay dung mang a cho matri res
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
res.a[i][j] = 0;
// xay dung a[i][j] cho res
for (int k = 0; k < n; k++)
{
res.a[i][j] = (res.a[i][j] + (x.a[i][k] * y.a[k][j]) % mod) % mod;
// res.a[i][j] %= mod;
}
}
}
return res;
}
};
matrix powmod(matrix A, long n, long k)
{
if (k == 1)
return A;
matrix x = powmod(A, n, k / 2);
if (k % 2 == 0)
{
return x * x;
}
else
{
return x * x * A;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
matrix A;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> A.a[i][j];
}
}
matrix ans = powmod(A, n, k);
ll res = 0;
for (int i = 0; i < n; i++)
{
res += ans.a[i][n - 1 - i];
res %= mod;
}
cout << res << endl;
}
}