-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3.cpp
81 lines (69 loc) · 1.46 KB
/
q3.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
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef vector<vector<long long>> Matrix;
Matrix matMul(const Matrix &a, const Matrix &b)
{
int n = a.size();
Matrix result(n, vector<long long>(n, 0));
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 matPow(Matrix base, long long exp)
{
int n = base.size();
Matrix result(n, vector<long long>(n, 0));
for (int i = 0; i < n; i++)
{
result[i][i] = 1;
}
while (exp > 0)
{
if (exp % 2 == 1)
{
result = matMul(result, base);
}
base = matMul(base, base);
exp /= 2;
}
return result;
}
int main()
{
int n;
long long k;
cin >> n >> k;
vector<vector<int>> vec(n, vector<int>(n));
Matrix adj(n, vector<long long>(n, 0));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> vec[i][j];
if (vec[i][j])
{
adj[i][j] = 1;
}
}
}
Matrix result = matPow(adj, k);
long long ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
ans = (ans + result[i][j]) % MOD;
}
}
cout << ans << endl;
return 0;
}