-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR.cpp
More file actions
90 lines (81 loc) · 2.24 KB
/
R.cpp
File metadata and controls
90 lines (81 loc) · 2.24 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// nhi btane ka h bhidu
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define w(t) while(t--)
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define PI 3.14159265358979323846
#define el cout<<endl;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
const ll MAX=3e6+10;
const ll INF=2e18;
#define INPUT_OUTPUT {\
freopen("input.txt","r",stdin);\
freopen("output.txt","w",stdout);\
}
/*
**************************************************************************
Concept -> Matrix Multiplication floyd warshall.
**************************************************************************
*/
// matrix multiplication
vector<vector<ll>> multiply(vector<vector<ll>>& A, vector<vector<ll>>& B) {
vector<vector<ll>> Arr(A.size(), vector<ll> (A.size(), 0));
int n = A.size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++) {
Arr[i][j] = (Arr[i][j] + (A[i][k] * B[k][j]) % mod) % mod;
}
}
}
return Arr;
}
// concept of divie & conquer for fast exponentation.
vector<vector<ll>> matrixExponentation(vector<vector<ll>> &arr, ll k) {
if(k == 0) {
vector<vector<ll>> A(arr.size(), vector<ll> (arr.size(), 0));
int n = arr.size();
for(int i = 0; i < n; i++) {
A[i][i] = 1;
}
return A;
}
vector<vector<ll>> res = matrixExponentation(arr, k / 2);
res = multiply(res, res);
if(k & 1) res = multiply(res, arr);
return res;
}
// logical code of program starts here.
void solve(int T) {
ll n, k;
cin>>n>>k;
vector<vector<ll>> arr(n, vector<ll> (n, 0));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin>>arr[i][j];
}
}
vector<vector<ll>> A = matrixExponentation(arr, k);
ll ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
ans = (ans + A[i][j]) % mod;
}
}
cout<<ans<<endl;
}
// mian function
int main() {
#ifndef ONLINE_JUDGE
INPUT_OUTPUT;//file handlings
#endif
fastIO;// fast input output
int t;
t=1;
//cin>>t;
for(int i = 1; i <= t; i++) solve(i);
return 0;
}