-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmat.cpp
More file actions
77 lines (74 loc) · 1.37 KB
/
Copy pathmat.cpp
File metadata and controls
77 lines (74 loc) · 1.37 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
long long int mod = 1e9+7;
template<typename T>
class mat{
public:
int r, c;
vector<vector<T> > m;
mat(int rr=0, int cc=0){ //be sure to call te constructor to set r and c(must!!!!?? very important)
m.resize(rr);
for(auto &v: m){
v.resize(cc);
}
r = rr;
c = cc;
}
void makeidentity(){
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(i==j) m[i][j] = 1;
else m[i][j] = 0;
}
}
}
mat operator + (const mat &o) const {
int row = o.r;
int col = o.c;
mat res(row, col);
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
res.m[i][j] = (m[i][j] + o.m[i][j])%mod;
}
}
return res;
}
mat operator - (const mat &o) const {
int row = o.r;
int col = o.c;
mat res(row, col);
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
res.m[i][j] = (m[i][j] - o.m[i][j])%mod;
res.m[i][j] = (res.m[i][j]+mod+mod)%mod;
}
}
return res;
}
mat operator * (const mat &o) const {
//int row = o.r;
int col = o.c;
mat res(r, col);
for(int i=0; i<r; i++){
for(int j=0; j<col; j++){
T all = 0;
for(int k=0; k<c; k++){
all = (all+m[i][k]*o.m[k][j])%mod;
}
res.m[i][j] = all;
}
}
return res;
}
mat operator ^ (long long int b) const {
mat<T> res(r, c);
res.makeidentity();
mat<T> gg = *this;
while(b){
if(b%2){
res = (res*gg);
}
gg = gg*gg;
b/=2;
}
return res;
}
};