-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_expo.cpp
More file actions
72 lines (57 loc) · 1.56 KB
/
matrix_expo.cpp
File metadata and controls
72 lines (57 loc) · 1.56 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
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
/*
-> Matrix Exponentiation
-> ref: http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html
-> further reading:
1. https://codeforces.com/blog/entry/43225
2. https://codeforces.com/blog/entry/80195
*/
const int SZ = 4;
void display(int mat[SZ][SZ]) {
for (int i = 0; i < SZ; ++i) {
for (int j = 0; j < SZ; ++j)
cout << mat[i][j] << " ";
cout << "\n";
}
}
void multiply(int a[SZ][SZ], int b[SZ][SZ]) {
int res[SZ][SZ];
for (int i = 0; i < SZ; ++i) {
for (int j = 0; j < SZ; ++j) {
res[i][j] = 0;
for (int k = 0; k < SZ; ++k)
res[i][j] = add(res[i][j], mul(a[i][k] % MOD, b[k][j] % MOD));
}
}
for (int i = 0; i < SZ; ++i) {
for (int j = 0; j < SZ; ++j)
a[i][j] = res[i][j];
}
}
void power(int x[SZ][SZ], int n) {
int res[SZ][SZ];
mem(res, 0);
for (int i = 0; i < SZ; ++i)
res[i][i] = 1;
for (; n; n /= 2) {
if (n % 2)
multiply(res, x);
multiply(x, x);
}
for (int i = 0; i < SZ; ++i) {
for (int j = 0; j < SZ; ++j)
x[i][j] = res[i][j];
}
}
int NthTerm(int n) {
if (n == 0) return 1;
if (n == 1) return 2;
//transformation matrix
int trans[SZ][SZ] = {};
//initial case
int base[SZ][1] = {};
power(trans, n - 1);
int res = 0;
for (int i = 0; i < SZ; ++i)
res = add(res, mul(trans[0][i], base[i][0]));
return res;
}