Created
November 16, 2015 11:15
-
-
Save sentenzo/b4ad1216e1e08f56f27f to your computer and use it in GitHub Desktop.
Strange and pointless algorithmic training
This file contains 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> | |
using namespace std; | |
int main() { | |
unsigned long long int fNum = 0; | |
unsigned int div = 0; | |
cin >> fNum >> div; | |
//fNum = 11527523930876953; | |
//div = 26673; | |
unsigned int f1, f2, tmp; | |
f1 = 0; f2 = 1; | |
tmp = 0; | |
int i = 0; | |
while (true) { | |
i++; | |
tmp = f1; | |
f1 = f2; | |
f2 +=tmp; | |
if (i == fNum) { | |
cout << f1; | |
return 0; | |
} | |
if (f2 >= div) | |
break; | |
} | |
//cout << "1\n"; | |
int i0 = ++i; | |
tmp = f1; | |
f1 = f2; | |
f2 += tmp; | |
f1 %= div; f2 %= div; | |
unsigned int f10, f20; | |
f10 = f1; f20 = f2; | |
while (true) { | |
i++; | |
tmp = f1; | |
f1 = f2; | |
f2 += tmp; | |
f1 %= div; f2 %= div; | |
//if (f1 == 10552) cout << "yes!\n"; | |
if (f1 == f10 && f2 == f20) | |
break; | |
} | |
//cout << "2\n"; | |
int t = i - i0; | |
int n = (fNum - i0)%t; | |
for(int j = 0; j < n; j++) { | |
tmp = f1; | |
f1 = f2; | |
f2 += tmp; | |
f1 %= div; f2 %= div; | |
//cout << f1 << "\t" << f2 << "\n"; | |
} | |
cout << f1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment