// Michał Wiatrowski #include <iostream> #include <vector> #include <stack> #include <string> #include <queue> using namespace std; typedef unsigned long long uint64; typedef long long int64; int64 A = 0x7fffffffffffff01; int64 B = 0x7fffffffffffff02; int64 M = 0x7fffffffffffff03; // For simplicity it is assumed x, y, and m are all positive. int64 addmod(int64 x, int64 y, int64 m) { x %= m; y %= m; int64 sum = x - m + y; // -m <= sum < m-1 return sum < 0 ? sum + m : sum; } int64 timesmod(int64 x, int64 y, int64 m) { x %= m; y %= m; int64 a = x < y ? x : y; // min int64 b = x < y ? y : x; // max int64 product = 0; for (; a != 0; a >>= 1, b = addmod(b,b,m) ) if (a&1) product = addmod(product,b,m); return product; } uint64 fibonacci(uint64 index, uint64 modulo) { if (index == 0) return 0; if (index == 1) return 1; index--; uint64 a = 1, b = 1, c = 1, d = 0; stack<char> s; while (index > 1) { if (index % 2 == 0) { index /= 2; s.push('d'); } else { index -= 1; s.push('m'); } } while (!s.empty()) { uint64 aa = a, bb = b, cc = c, dd = d; char cmd = s.top(); s.pop(); if (cmd == 'd') { a = (timesmod(aa, aa, modulo) + timesmod(bb, cc, modulo)) % modulo; b = (timesmod(aa, bb, modulo) + timesmod(bb, dd, modulo)) % modulo; c = (timesmod(aa, cc, modulo) + timesmod(cc, dd, modulo)) % modulo; d = (timesmod(cc, bb, modulo) + timesmod(dd, dd, modulo)) % modulo; } else { a = (aa + bb) % modulo; b = aa; c = (cc + dd) % modulo; d = cc; } } return (c + d) % modulo; } int main() { ios_base::sync_with_stdio(false); string napis; cin >> napis; uint64 liczba = 0; uint64 obciecie = 1; for (char c : napis) { liczba *= 10; liczba += (c - '0'); obciecie *= 10; } int wykladnik = napis.size(); uint64 mod = 1; for (int i = 0; i < wykladnik; ++i) mod *= 10; if (liczba == 0 && wykladnik == 1) { cout << 0 << endl; return 0; } uint64 wynik = 0; queue<pair<uint64, uint64>> stany; for (int i = 0; i < 60; ++i) { if (fibonacci(i, 10) == liczba % 10) stany.push(make_pair(i, 10)); } while (!stany.empty()) { uint64 index = stany.front().first; uint64 modulo = stany.front().second; stany.pop(); if (wykladnik == 1) { if (fibonacci(index, mod) == liczba % 10) { wynik = index; break; } } uint64 duzyCykl = modulo * 15; uint64 malyCykl = modulo * 15 / 10; if (modulo == 10) { malyCykl = 60; duzyCykl = 300; } if (modulo == 100) { malyCykl = 300; } for (uint64 i = 0; i < (duzyCykl / malyCykl); ++i) { if (i * malyCykl > 1500000000000000000 - index) continue; uint64 kandytat = index + i * malyCykl; uint64 noweModulo = modulo * 10; if (fibonacci(kandytat, noweModulo) == liczba % noweModulo) { if (noweModulo == mod) { wynik = kandytat; break; } else { stany.push(make_pair(kandytat, noweModulo)); } } } } if (wynik != 0) cout << wynik; else cout << "NIE" << endl; 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | // Michał Wiatrowski #include <iostream> #include <vector> #include <stack> #include <string> #include <queue> using namespace std; typedef unsigned long long uint64; typedef long long int64; int64 A = 0x7fffffffffffff01; int64 B = 0x7fffffffffffff02; int64 M = 0x7fffffffffffff03; // For simplicity it is assumed x, y, and m are all positive. int64 addmod(int64 x, int64 y, int64 m) { x %= m; y %= m; int64 sum = x - m + y; // -m <= sum < m-1 return sum < 0 ? sum + m : sum; } int64 timesmod(int64 x, int64 y, int64 m) { x %= m; y %= m; int64 a = x < y ? x : y; // min int64 b = x < y ? y : x; // max int64 product = 0; for (; a != 0; a >>= 1, b = addmod(b,b,m) ) if (a&1) product = addmod(product,b,m); return product; } uint64 fibonacci(uint64 index, uint64 modulo) { if (index == 0) return 0; if (index == 1) return 1; index--; uint64 a = 1, b = 1, c = 1, d = 0; stack<char> s; while (index > 1) { if (index % 2 == 0) { index /= 2; s.push('d'); } else { index -= 1; s.push('m'); } } while (!s.empty()) { uint64 aa = a, bb = b, cc = c, dd = d; char cmd = s.top(); s.pop(); if (cmd == 'd') { a = (timesmod(aa, aa, modulo) + timesmod(bb, cc, modulo)) % modulo; b = (timesmod(aa, bb, modulo) + timesmod(bb, dd, modulo)) % modulo; c = (timesmod(aa, cc, modulo) + timesmod(cc, dd, modulo)) % modulo; d = (timesmod(cc, bb, modulo) + timesmod(dd, dd, modulo)) % modulo; } else { a = (aa + bb) % modulo; b = aa; c = (cc + dd) % modulo; d = cc; } } return (c + d) % modulo; } int main() { ios_base::sync_with_stdio(false); string napis; cin >> napis; uint64 liczba = 0; uint64 obciecie = 1; for (char c : napis) { liczba *= 10; liczba += (c - '0'); obciecie *= 10; } int wykladnik = napis.size(); uint64 mod = 1; for (int i = 0; i < wykladnik; ++i) mod *= 10; if (liczba == 0 && wykladnik == 1) { cout << 0 << endl; return 0; } uint64 wynik = 0; queue<pair<uint64, uint64>> stany; for (int i = 0; i < 60; ++i) { if (fibonacci(i, 10) == liczba % 10) stany.push(make_pair(i, 10)); } while (!stany.empty()) { uint64 index = stany.front().first; uint64 modulo = stany.front().second; stany.pop(); if (wykladnik == 1) { if (fibonacci(index, mod) == liczba % 10) { wynik = index; break; } } uint64 duzyCykl = modulo * 15; uint64 malyCykl = modulo * 15 / 10; if (modulo == 10) { malyCykl = 60; duzyCykl = 300; } if (modulo == 100) { malyCykl = 300; } for (uint64 i = 0; i < (duzyCykl / malyCykl); ++i) { if (i * malyCykl > 1500000000000000000 - index) continue; uint64 kandytat = index + i * malyCykl; uint64 noweModulo = modulo * 10; if (fibonacci(kandytat, noweModulo) == liczba % noweModulo) { if (noweModulo == mod) { wynik = kandytat; break; } else { stany.push(make_pair(kandytat, noweModulo)); } } } } if (wynik != 0) cout << wynik; else cout << "NIE" << endl; return 0; } |