-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathR.cpp
More file actions
51 lines (45 loc) 路 1021 Bytes
/
Copy pathR.cpp
File metadata and controls
51 lines (45 loc) 路 1021 Bytes
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = std::vector<long long>;
ll mod = 1e9 + 7;
ll MAX = 1e9;
struct matrix
{
ll n;
ll mat[50][50];
};
matrix multiply(matrix a, matrix b)
{
matrix res;
res.n = a.n;
for (ll i = 0; i < res.n; i++)
for (ll j = 0; j < res.n; j++)
{
res.mat[i][j] = 0;
for (ll k = 0; k < a.n; k++) res.mat[i][j] = (res.mat[i][j] + (a.mat[i][k] * b.mat[k][j]) % mod) % mod;
}
return res;
}
matrix power(matrix mat, ll p)
{
if (p == 1) return mat;
matrix res = power(mat, p / 2);
res = multiply(res, res);
if (p & 1) res = multiply(res, mat);
return res;
}
signed main() {
ll n, k;
cin >> n >> k;
matrix M;
M.n = n;
for (ll i = 0; i < n; i++) for (ll j = 0; j < n; j++) cin >> M.mat[i][j];
ll ans = 0;
matrix res = power(M, k);
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
ans = (ans + res.mat[i][j]) % mod;
cout << ans;
return 0;
}