-
Notifications
You must be signed in to change notification settings - Fork 0
/
martix.cpp
91 lines (91 loc) · 1.41 KB
/
martix.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
82
83
84
85
86
87
88
89
90
91
class matrix{
public:
int n;
vector<vi> val;
matrix(int n=2);
matrix operator+(const matrix&) const;
matrix operator-(const matrix&) const;
matrix operator*(const matrix&) const;
matrix& operator=(const matrix& b){
val=b.val;
n=b.n;
return *this;
}
void set(int x){
rep(i,0,n){
rep(j,0,n){
val[i][j]=x;
}
}
}
void print(){
rep(i,0,n){
rep(j,0,n){
cout<<val[i][j]<<" ";
}
cout<<endl;
}
}
matrix mul(int a){
matrix ans(this->n);
rep(i,0,this->n){
rep(j,0,this->n){
ans.val[i][j]=a*this->val[i][j];
}
}
return ans;
}
};
matrix::matrix(int x){
n=x;
val.resize(x);
rep(i,0,x){
val[i].resize(x);
}
}
matrix matrix::operator+(const matrix& b) const{
matrix ans(b.n);
rep(i,0,b.n){
rep(j,0,b.n){
ans.val[i][j]=this->val[i][j]+b.val[i][j];
}
}
return ans;
}
matrix matrix::operator-(const matrix& b) const{
matrix ans(b.n);
rep(i,0,b.n){
rep(j,0,b.n){
ans.val[i][j]=this->val[i][j]-b.val[i][j];
}
}
return ans;
}
matrix matrix::operator*(const matrix& b) const{
matrix ans(b.n);
rep(i,0,b.n){
rep(j,0,b.n){
rep(k,0,b.n){
ans.val[i][j]+=this->val[i][k]*b.val[k][j];
}
}
}
return ans;
}
matrix identity(int N=2){
matrix ans(N);
rep(i,0,N){
ans.val[i][i]=1;
}
return ans;
}
matrix matexpo(matrix a, int n){
if(n==0){
return identity(a.n);
}
if(n==1) return a;
matrix x=matexpo(a,n/2);
matrix r=x*x;
if(n&1) r=r*a;
return r;
}