-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
fibonacci_matrix_exponentiation.cpp
116 lines (110 loc) · 3.52 KB
/
fibonacci_matrix_exponentiation.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @file
* @brief This program computes the N^th Fibonacci number in modulo mod
* input argument .
*
* Takes O(logn) time to compute nth Fibonacci number
*
*
* \author [villayatali123](https://github.com/villayatali123)
* \author [unknown author]()
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp,
* fibonacci_large.cpp
*/
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
/**
* This function finds nth fibonacci number in a given modulus
* @param n nth fibonacci number
* @param mod modulo number
*/
uint64_t fibo(uint64_t n, uint64_t mod) {
std::vector<uint64_t> result(2, 0);
std::vector<std::vector<uint64_t>> transition(2,
std::vector<uint64_t>(2, 0));
std::vector<std::vector<uint64_t>> Identity(2, std::vector<uint64_t>(2, 0));
n--;
result[0] = 1, result[1] = 1;
Identity[0][0] = 1;
Identity[0][1] = 0;
Identity[1][0] = 0;
Identity[1][1] = 1;
transition[0][0] = 0;
transition[1][0] = transition[1][1] = transition[0][1] = 1;
while (n) {
if (n % 2) {
std::vector<std::vector<uint64_t>> res(2,
std::vector<uint64_t>(2, 0));
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
res[i][j] =
(res[i][j] % mod +
((Identity[i][k] % mod * transition[k][j] % mod)) %
mod) %
mod;
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Identity[i][j] = res[i][j];
}
}
n--;
} else {
std::vector<std::vector<uint64_t>> res1(
2, std::vector<uint64_t>(2, 0));
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
res1[i][j] =
(res1[i][j] % mod + ((transition[i][k] % mod *
transition[k][j] % mod)) %
mod) %
mod;
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
transition[i][j] = res1[i][j];
}
}
n = n / 2;
}
}
return ((result[0] % mod * Identity[0][0] % mod) % mod +
(result[1] % mod * Identity[1][0] % mod) % mod) %
mod;
}
/**
* Function to test above algorithm
*/
static void test() {
assert(fibo(6, 1000000007) == 8);
std::cout << "test case:1 passed\n";
assert(fibo(5, 1000000007) == 5);
std::cout << "test case:2 passed\n";
assert(fibo(10, 1000000007) == 55);
std::cout << "test case:3 passed\n";
assert(fibo(500, 100) == 25);
std::cout << "test case:3 passed\n";
assert(fibo(500, 10000) == 4125);
std::cout << "test case:3 passed\n";
std::cout << "--All tests passed--\n";
}
/**
* Main function
*/
int main() {
test();
uint64_t mod = 1000000007;
std::cout << "Enter the value of N: ";
uint64_t n = 0;
std::cin >> n;
std::cout << n << "th Fibonacci number in modulo " << mod << ": "
<< fibo(n, mod) << std::endl;
}