Created
July 19, 2016 05:49
-
-
Save aadimator/4950f34fb476382a4a49eb86395726ce to your computer and use it in GitHub Desktop.
Huge Fibonacci Number modulo m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
long long get_pisano_period(long long m) { | |
long long a = 0, b = 1, c = a + b; | |
for (int i = 0; i < m * m; i++) { | |
c = (a + b) % m; | |
a = b; | |
b = c; | |
if (a == 0 && b == 1) return i + 1; | |
} | |
} | |
long long get_fibonacci_huge(long long n, long long m) { | |
long long remainder = n % get_pisano_period(m); | |
long long first = 0; | |
long long second = 1; | |
long long res = remainder; | |
for (int i = 1; i < remainder; i++) { | |
res = (first + second) % m; | |
first = second; | |
second = res; | |
} | |
return res % m; | |
} | |
int main() { | |
// for (int i = 2; i < 100; i++) { | |
// std::cout << i << " : " << get_pisano_period(i) << std::endl; | |
// } | |
// return 0; | |
long long n, m; | |
std::cin >> n >> m; | |
std::cout << get_fibonacci_huge(n, m) << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment