-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
76 lines (67 loc) · 1.55 KB
/
Matrix.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
struct matrix {
//add mod at last if there is negative number
vector <VLL> v;
int x, y;
matrix(int n = 0, int m = 0, int value = 0) : x(n), y(m) {
v = vector<VLL>(n, VLL(m, 0));
if(value == 1) {
REP(i, 0, n)
{
v[i][i] = 1;
}
}
}
matrix operator*(const matrix &o) const {
matrix ans(x, o.y);
REP(i, 0, x)
{
REP(k, 0, y)
{
REP(j, 0, o.y)
{
ans.v[i][j] = (ans.v[i][j] + v[i][k] * o.v[k][j]) % mod;
}
}
}
return ans;
}
matrix operator+(const matrix &o) const {
assert(x == o.x && y == o.y);
matrix ans = *this;
REP(i, 0, x)
{
REP(j, 0, y)
{
ans.v[i][j] += o.v[i][j];
ans.v[i][j] %= mod;
}
}
return ans;
}
friend matrix fast(matrix a, ll b) {
matrix ans(a.x, a.y, 1);
assert(a.x == a.y);
while(b) {
if(b & 1) {
b--;
ans = ans * a;
} else {
b /= 2;
a = a * a;
}
}
return ans;
}
void print() {
printf("row = %d, column = %d\n", x, y);
REP(i, 0, x)
{
REP(j, 0, y)
{
cout << v[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
};