-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMATRIX.cpp
More file actions
44 lines (44 loc) · 1.28 KB
/
MATRIX.cpp
File metadata and controls
44 lines (44 loc) · 1.28 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct MATRIX{
#define MAXN 105
int mod=1e9+7;
ll data[MAXN][MAXN],n;
MATRIX(int siz){this->n=siz;for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)data[i][j]=0;}
MATRIX operator*(const MATRIX& b){
MATRIX ans(n);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++){
(ans.data[i][j]+=data[i][k]*b.data[k][j]%mod)%=mod;
}
return ans;
}
MATRIX(const MATRIX& b){
this->n=b.n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
data[i][j]=b.data[i][j];
}
MATRIX& operator=(const MATRIX& b){
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) data[i][j]=b.data[i][j];
return *this;
}
MATRIX operator+(const MATRIX& b){
MATRIX ans(n);
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) (ans.data[i][j]+=data[i][j]+b.data[i][j])%=mod;
}
MATRIX& operator*=(const MATRIX& b){
return *this=*this*b;
}
MATRIX& operator+=(const MATRIX& b){
return *this=*this+b;
}
MATRIX fpow(ll b){
MATRIX ans(n),a(*this);
for(int i=1;i<=n;i++) ans.data[i][i]=1;
for(;b;b>>=1,a=a*a) {
if(b&1) ans=ans*a;
}return ans;
}
#undef MAXN
};