-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix2.cpp
64 lines (62 loc) · 1.12 KB
/
matrix2.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
struct Matrix
{
ll val[2][2];
ll n_;
Matrix (ll n=2) : n_(n)
{
}
void print()
{
for(ll i=0;i<n_;++i)
{
for(ll j=0;j<n_;++j)
cout<<val[i][j]<<" ";
cout<<"\n";
}
}
void set(ll x)
{
for(ll i=0;i<n_;++i)
for(ll j=0;j<n_;++j)
val[i][j]=x;
}
Matrix operator*(const Matrix& b) const
{
ll n=n_;
Matrix ans(n);
ans.set(0);
for(ll i=0;i<n_;++i)
{
for(ll j=0;j<n_;++j)
{
ans.val[i][j]=0;
for(ll k=0;k<n_;++k)
{
ans.val[i][j]+=val[i][k]*b.val[k][j];
ans.val[i][j]%=MOD;
}
}
}
return ans;
}
};
Matrix I(ll n)
{
Matrix Iden(n);
Iden.set(0);
for(ll i=0;i<n;++i)
Iden.val[i][i]=1;
return Iden;
}
Matrix power(Matrix m,ll pw)
{
if(pw==0)
return I(m.n_);
if(pw==1)
return m;
Matrix t=power(m,pw/2);
t=t*t;
if(pw&1)
t=t*m;
return t;
}