-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
61 lines (53 loc) · 971 Bytes
/
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
typedef long long int64;
// const int64 M = 1000000007;
class matrix
{
public:
matrix(int n) : n(n), A(n, vector<int64>(n)) {}
int64& operator()(int64 i, int64 j) { return A[i][j]; }
int64 operator()(int64 i, int64 j) const { return A[i][j]; }
int64 size() const { return n; }
private:
int n;
vector<vector<int64>> A;
};
matrix id(int64 n)
{
matrix I(n);
for (int i = 0; i < n; i++)
I(i, i) = 1;
return I;
}
matrix operator *(const matrix& A, const matrix& B)
{
int64 n = A.size();
matrix C(n);
for (int64 i = 0; i < n; i++)
for (int64 j = 0; j < n; j++)
for (int64 k = 0; k < n; k++)
{
C(i, j) += A(i, k) * B(k, j); // % M;
//C(i, j) %= M;
}
return C;
}
matrix operator ^(const matrix& A, int64 m)
{
int64 n = A.size();
matrix R = id(n);
matrix B = A;
while (m)
{
if (m & 1)
{
m--;
R = R * B;
}
else
{
m >>= 1;
B = B * B;
}
}
return R;
}