-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixExponentiation.cpp
67 lines (49 loc) · 1.29 KB
/
matrixExponentiation.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
const ll mod = 1e9+7;
using matrix = vector<vector<ll>>;
int k; //size of square matrix
// computes A x B
matrix mult(matrix A, matrix B) {
matrix C(k+1, vector<ll>(k+1));
for(int i = 1; i <= k; i++) {
for(int j = 1; j <= k; ++j) {
for(int l = 1; l <= k; ++l) {
C[i][j] = (C[i][j] + A[i][l] * B[l][j]) % mod;
}
}
}
return C;
}
// compute A ^ p
matrix power(matrix A, int p) {
if(p == 1) {
return A;
} else if(p % 2 == 1) {
return mult(A, power(A, p-1));
} else {
matrix X = power(A, p/2);
return mult(X, X);
}
}
int main() {
int n; //the fifth Fibonacci number
k = 2;
while(scanf("%d", &n) == 1) {
vector<ll> F(k+1, 0);
F[1] = 1;
F[2] = 1;
matrix T(k+1, vector<ll>(k+1, 0));
T[1][1] = 1; T[1][2] = 1;
T[2][1] = 1; T[2][2] = 0;
if(n == 1) {
printf("1\n");
}
// raise to the power of (n-1)th
T = power(T, n-2);
ll ans = 0; // first column of new states matrix
for(int i = 0; i <= k; ++i) {
ans += (T[1][i] * F[i]) % mod;
}
printf("%lld\n", ans);
}
return 0;
}