-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathfib_huge.cpp
51 lines (39 loc) · 1.08 KB
/
fib_huge.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
#include <iostream>
using std::cin;
using std::cout;
long long get_fibonacci_huge_naive(long long n, long long m) {
if (n <= 1)
return n;
long long previous = 0;
long long current = 1;
for (long long i = 0; i < n - 1; ++i) {
long long tmp_previous = previous;
previous = current;
current = tmp_previous + current;
}
return current % m;
}
long long get_pisano_period_length(long long m) {
long long F1 = 0, F2 = 1, F = F1 + F2;
for (int i = 0; i < m * m; i++) {
F = (F1 + F2) % m;
F1 = F2;
F2 = F;
if (F1 == 0 && F2 == 1) return i + 1;
}
}
long long get_fibonacci_huge_fast(long long n, long long m) {
long long remainder = n % get_pisano_period_length(m);
long long F1 = 0, F2 = 1, F = remainder;
for (int i = 1; i < remainder; i++) {
F = (F1 + F2) % m;
F1 = F2;
F2 = F;
}
return F % m;
}
int main() {
long long n, m;
std::cin >> n >> m;
std::cout << get_fibonacci_huge_fast(n, m) << '\n';
}