// Grzegorz Bukowiec // PA 2015 - Zadanie 2A - Fibonacci #include <iostream> #include <string> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); string str; cin >> str; long long max = 1; long long r = 0; for (int i = 0; i < str.length(); ++i) { max *= 10; r = r * 10 + static_cast<long long>(str[i] - '0'); } bool **check; check = new bool*[max]; for (long long i = 0; i < max; ++i) { check[i] = new bool[max]; for (long long j = 0; j < max; ++j) { check[i][j] = false; } } long long a = 0; long long b = 1; long long i = 1; bool ok = str[0] != '0' || str.length() == 1; if (ok && r == 0) { cout << 0; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } if (ok && r == 1) { cout << a; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } check[0][1] = true; while (true) { ++i; long long c = a + b; if (c >= max) { ok = true; c %= max; } a = b; b = c; if (check[a][b]) { cout << "NIE"; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } check[a][b] = true; if (ok && b == r) { cout << i; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } } }
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | // Grzegorz Bukowiec // PA 2015 - Zadanie 2A - Fibonacci #include <iostream> #include <string> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); string str; cin >> str; long long max = 1; long long r = 0; for (int i = 0; i < str.length(); ++i) { max *= 10; r = r * 10 + static_cast<long long>(str[i] - '0'); } bool **check; check = new bool*[max]; for (long long i = 0; i < max; ++i) { check[i] = new bool[max]; for (long long j = 0; j < max; ++j) { check[i][j] = false; } } long long a = 0; long long b = 1; long long i = 1; bool ok = str[0] != '0' || str.length() == 1; if (ok && r == 0) { cout << 0; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } if (ok && r == 1) { cout << a; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } check[0][1] = true; while (true) { ++i; long long c = a + b; if (c >= max) { ok = true; c %= max; } a = b; b = c; if (check[a][b]) { cout << "NIE"; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } check[a][b] = true; if (ok && b == r) { cout << i; for (long long i = 0; i < max; ++i) { delete [] check[i]; } delete [] check; return 0; } } } |