-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiboHugeMod.cpp
More file actions
67 lines (49 loc) · 932 Bytes
/
fiboHugeMod.cpp
File metadata and controls
67 lines (49 loc) · 932 Bytes
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
#include <iostream>
using namespace std;
void calculate_next(int& a, int& b, int& c, int m)
{
c = (a+b) % m;
a = b;
b = c;
}
int fibonacci(int n, int m)
{
//gives the nth fibonacci number
int a = 0;
int b = 1;
int c = n;
for(int i = 1; i < n; i++)
{
calculate_next(a, b, c, m);
}
return c;
}
int pisano_length(int m)
{
if(m == 2) return 3;
//Upper bound: π(m)≤6m with equality iff m=2×5n for n=1,2,3,…. [Attributed to K. S. Brown, proof]
int max_pisano = 6*m;
int a = 0;
int b = 1;
int c = 1;
for(int i = 2; i < max_pisano; i++)
{
calculate_next(a, b, c, m);
if(a == 0 && b == 1) return i-1;
}
return 1;
}
int n_mod_m(long long n, int m)
{
int pisano = pisano_length(m);
int n_prime = n % pisano;
int fibo_at_n_prime_mod_m = fibonacci(n_prime, m);
return fibo_at_n_prime_mod_m;
}
int main()
{
long long n =0;
int m = 0;
cin >> n >> m;
cout << n_mod_m(n, m) << endl;
}