#include <cstdio> #include <set> #include <cstdint> #include <cinttypes> #include <utility> #include <cassert> using namespace std; struct uint128 { uint64_t top, low; uint128(uint64_t top, uint64_t low) : top(top), low(low) {} uint128(uint64_t low) : top(0), low(low) {} uint64_t operator%=(uint64_t mod); uint128& operator+=(uint64_t a); uint128& operator=(const uint128& o); }; uint128& uint128::operator=(const uint128& o) { top = o.top; low = o.low; return *this; } uint128& uint128::operator+=(uint64_t a) { uint64_t new_low = low + a; if (new_low < low || new_low < a) { top++; } low = new_low; return *this; } uint64_t uint128::operator%=(uint64_t mod) { if (top == 0) { low %= mod; return low; } unsigned long long top_mod = 0, low_mod = mod; unsigned long long top_x = 0, low_x = 1; while ((top > top_mod || (top == top_mod && low >= low_mod)) && (top_mod & (1ULL << 63)) == 0) { // mod <<= 1 top_mod = (top_mod << 1) + (low_mod >> 63); low_mod <<= 1; // x <<= 1 top_x = (top_x << 1) + (low_x >> 63); low_x <<= 1; } while (top_x != 0 || low_x != 0) { if (top > top_mod || (top == top_mod && low >= low_mod)) { // res -= mod if (low < low_mod) { assert(top >= top_mod + 1); top--; } low -= low_mod; top -= top_mod; } // x >>= 1 low_x = (low_x >> 1) + ((top_x & 1) << 63); top_x >>= 1; // mod >>= 1 low_mod = (low_mod >> 1) + ((top_mod & 1) << 63); top_mod >>= 1; } assert(top == 0); return low; } uint64_t mul_mod(uint64_t a, uint64_t b, uint64_t mod) { static const uint64_t MASK_L = (1ULL << 32) - 1; a %= mod; b %= mod; uint64_t top_a = a >> 32, low_a = a & MASK_L, top_b = b >> 32, low_b = b & MASK_L; uint64_t top_res = top_a * top_b + ((top_a * low_b + low_a * top_b + ((low_a * low_b)>>32)) >> 32); uint64_t low_res = ((top_a * low_b + low_a * top_b) << 32) + low_a * low_b; uint128 res(top_res, low_res); return (res %= mod); } uint64_t fib(uint64_t num) { uint64_t modulo = 1000000000000000000ULL; uint64_t mask = 1; while (mask <= num) { mask <<= 1; } mask >>= 1; uint64_t a = 1, b = 0, c = 1; while (mask > 0) { uint64_t A = (mul_mod(a, a, modulo) + mul_mod(b, b, modulo))%modulo, B = (mul_mod(a, b, modulo) + mul_mod(b, c, modulo))%modulo, C = (mul_mod(b, b, modulo) + mul_mod(c, c, modulo))%modulo; if (num & mask) { a = (A + B) % modulo; b = A; c = B; } else { a = A; b = B; c = C; } mask >>= 1; } return b; } int main() { char input[19]; scanf("%s", input); uint64_t num = 0; int num_digits = 0; while (input[num_digits] != '\0') { num = num * 10 + input[num_digits] - '0'; num_digits++; } if (num_digits <= 4) { uint64_t modulo = 1; for (int i = 0; i < num_digits; ++i) { modulo *= 10; } uint64_t a = 0, b = 1; set<pair<int, int>> pairs; bool put = false; int i =0; while (true) { if (a >= modulo / 10) { put = true; } if (put) { if (a == num) { printf("%d\n", i); return 0; } pair<int, int> p = make_pair(a, b); if (pairs.count(p) > 0) { break; } pairs.insert(p); } int tmp = a; a = b; b += tmp; b %= modulo; i++; } printf("NIE\n"); return 0; } else { const unsigned long long M = 10000000000000000000ULL; uint128 a(0), b(1); uint64_t i = 0; for (; i < 100; ++i) { uint128 A = b; b += a.low; a = A; a %= M; b %= M; } uint64_t mod = 10000; for (; i < 16000; ++i) { if(a.low % mod == num % mod) { break; } uint128 A = b; b += a.low; a = A; a %= M; b %= M; } if (i == 16000) { printf("NIE\n"); return 0; } uint64_t mul = 7500; for (int d = 5; d <= num_digits; ++d) { mod *= 10; int j = 0; for (; j < 20; ++j) { if (fib(i) % mod == num % mod) { break; } i += mul; } mul *= 10; } if (num % 4 == 3 || num % 8 == 0 || num % 4 == 1 || num % 32 == 2) { printf("%" PRIu64 "\n", i); } else { printf("NIE\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 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #include <cstdio> #include <set> #include <cstdint> #include <cinttypes> #include <utility> #include <cassert> using namespace std; struct uint128 { uint64_t top, low; uint128(uint64_t top, uint64_t low) : top(top), low(low) {} uint128(uint64_t low) : top(0), low(low) {} uint64_t operator%=(uint64_t mod); uint128& operator+=(uint64_t a); uint128& operator=(const uint128& o); }; uint128& uint128::operator=(const uint128& o) { top = o.top; low = o.low; return *this; } uint128& uint128::operator+=(uint64_t a) { uint64_t new_low = low + a; if (new_low < low || new_low < a) { top++; } low = new_low; return *this; } uint64_t uint128::operator%=(uint64_t mod) { if (top == 0) { low %= mod; return low; } unsigned long long top_mod = 0, low_mod = mod; unsigned long long top_x = 0, low_x = 1; while ((top > top_mod || (top == top_mod && low >= low_mod)) && (top_mod & (1ULL << 63)) == 0) { // mod <<= 1 top_mod = (top_mod << 1) + (low_mod >> 63); low_mod <<= 1; // x <<= 1 top_x = (top_x << 1) + (low_x >> 63); low_x <<= 1; } while (top_x != 0 || low_x != 0) { if (top > top_mod || (top == top_mod && low >= low_mod)) { // res -= mod if (low < low_mod) { assert(top >= top_mod + 1); top--; } low -= low_mod; top -= top_mod; } // x >>= 1 low_x = (low_x >> 1) + ((top_x & 1) << 63); top_x >>= 1; // mod >>= 1 low_mod = (low_mod >> 1) + ((top_mod & 1) << 63); top_mod >>= 1; } assert(top == 0); return low; } uint64_t mul_mod(uint64_t a, uint64_t b, uint64_t mod) { static const uint64_t MASK_L = (1ULL << 32) - 1; a %= mod; b %= mod; uint64_t top_a = a >> 32, low_a = a & MASK_L, top_b = b >> 32, low_b = b & MASK_L; uint64_t top_res = top_a * top_b + ((top_a * low_b + low_a * top_b + ((low_a * low_b)>>32)) >> 32); uint64_t low_res = ((top_a * low_b + low_a * top_b) << 32) + low_a * low_b; uint128 res(top_res, low_res); return (res %= mod); } uint64_t fib(uint64_t num) { uint64_t modulo = 1000000000000000000ULL; uint64_t mask = 1; while (mask <= num) { mask <<= 1; } mask >>= 1; uint64_t a = 1, b = 0, c = 1; while (mask > 0) { uint64_t A = (mul_mod(a, a, modulo) + mul_mod(b, b, modulo))%modulo, B = (mul_mod(a, b, modulo) + mul_mod(b, c, modulo))%modulo, C = (mul_mod(b, b, modulo) + mul_mod(c, c, modulo))%modulo; if (num & mask) { a = (A + B) % modulo; b = A; c = B; } else { a = A; b = B; c = C; } mask >>= 1; } return b; } int main() { char input[19]; scanf("%s", input); uint64_t num = 0; int num_digits = 0; while (input[num_digits] != '\0') { num = num * 10 + input[num_digits] - '0'; num_digits++; } if (num_digits <= 4) { uint64_t modulo = 1; for (int i = 0; i < num_digits; ++i) { modulo *= 10; } uint64_t a = 0, b = 1; set<pair<int, int>> pairs; bool put = false; int i =0; while (true) { if (a >= modulo / 10) { put = true; } if (put) { if (a == num) { printf("%d\n", i); return 0; } pair<int, int> p = make_pair(a, b); if (pairs.count(p) > 0) { break; } pairs.insert(p); } int tmp = a; a = b; b += tmp; b %= modulo; i++; } printf("NIE\n"); return 0; } else { const unsigned long long M = 10000000000000000000ULL; uint128 a(0), b(1); uint64_t i = 0; for (; i < 100; ++i) { uint128 A = b; b += a.low; a = A; a %= M; b %= M; } uint64_t mod = 10000; for (; i < 16000; ++i) { if(a.low % mod == num % mod) { break; } uint128 A = b; b += a.low; a = A; a %= M; b %= M; } if (i == 16000) { printf("NIE\n"); return 0; } uint64_t mul = 7500; for (int d = 5; d <= num_digits; ++d) { mod *= 10; int j = 0; for (; j < 20; ++j) { if (fib(i) % mod == num % mod) { break; } i += mul; } mul *= 10; } if (num % 4 == 3 || num % 8 == 0 || num % 4 == 1 || num % 32 == 2) { printf("%" PRIu64 "\n", i); } else { printf("NIE\n"); } } } |