-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchia_va_tri_MATRIX.cpp
More file actions
62 lines (59 loc) · 1.11 KB
/
chia_va_tri_MATRIX.cpp
File metadata and controls
62 lines (59 loc) · 1.11 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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9+7;
long n, k;
struct matrix
{
ll a[1000][1000];
//Nap chong phep nhan matrix
friend matrix operator * (matrix x, matrix y)
{
matrix res;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
res.a[i][j] = 0;
for(int k = 0; k < n; k++)
{
res.a[i][j] += x.a[i][k] * y.a[k][j];
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()
{
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]%mod;
}
cout << res << endl;
}