-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3390_MatrixQuickPow.cpp
80 lines (68 loc) · 1.19 KB
/
P3390_MatrixQuickPow.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
#include <iostream>
#define ll long long
#define MOD 1000000007
using namespace std;
struct Matrix{
ll m[101][101];
};
ll n,k,num;
Matrix a,e;
Matrix mul(Matrix x,Matrix y){
Matrix cur;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cur.m[i][j]=0;
}
}
//go though our finalMartix
//each one of our finalMartix
//need to go though two other array
//so O(n^3)
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
cur.m[i][j]=cur.m[i][j]%MOD+x.m[i][k]*y.m[k][j]%MOD;
}
}
}
//current Martix
return cur;
}
Matrix pow(Matrix b,ll p){
//equal to int ans=1
//e is our special matrix
Matrix ans=e;
while(p){
if(p&1){
//ans*=b;
ans=mul(ans,b);
}
//b*=b;
b=mul(b,b);
p>>=1;
}
return ans;
}
int main(){
cin>>n>>k;
//input -> martix a
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) {
cin>>a.m[i][j];
}
}
//get our "1"
//Martix e times any Martix equals that Martix
for(int i=0;i<n;i++){
e.m[i][i]=1;
}
//POWER IT!
Matrix ans=pow(a,k);
//output
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
cout<<ans.m[i][j]%MOD<<" ";
cout<<endl;
}
return 0;
}