#include <iostream> #include <sstream> #include <string> #include <cmath> using namespace std; long long fib(long long search, int digits) { long long prev[] = {0, 1}; long long limit = pow(10, digits); for (long long i = 2; ; ++i) { int next = i % 2; prev[next] = (prev[0] + prev[1]) % limit; if (prev[next] == search) return i; if (prev[0] == 0 && prev[1] == 1) return -1; } } int main() { string str_digits; cin >> str_digits; stringstream ss(str_digits); long long patern; ss >> patern; long long res = fib(patern, str_digits.size()); if (res < 0) { cout << "NIE\n"; } else { cout << res << "\n"; } }
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 | #include <iostream> #include <sstream> #include <string> #include <cmath> using namespace std; long long fib(long long search, int digits) { long long prev[] = {0, 1}; long long limit = pow(10, digits); for (long long i = 2; ; ++i) { int next = i % 2; prev[next] = (prev[0] + prev[1]) % limit; if (prev[next] == search) return i; if (prev[0] == 0 && prev[1] == 1) return -1; } } int main() { string str_digits; cin >> str_digits; stringstream ss(str_digits); long long patern; ss >> patern; long long res = fib(patern, str_digits.size()); if (res < 0) { cout << "NIE\n"; } else { cout << res << "\n"; } } |