#include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> using namespace std; //#define DEBUG #ifdef DEBUG #define ASSERT(x) assert(x) #define DEBPRINT(...) fprintf(stderr, __VA_ARGS__) #else #define ASSERT(x) #define DEBPRINT(...) #endif typedef long long slng; slng pow10[19]; slng C; int N; #define BASE 1000000000LL // bigint modulo 10^18, kept positive. struct bigint { slng hi, lo; // 10^9 each. slng fib; // tag it along. }; // mul mod 10^18. struct bigint mul(struct bigint a, struct bigint b) { struct bigint ret = { a.hi * b.lo + b.hi * a.lo, a.lo * b.lo, 0 }; ret.hi += ret.lo / BASE; ret.lo %= BASE; ret.hi %= BASE; return ret; } struct bigint add(struct bigint a, struct bigint b) { struct bigint ret = { a.hi + b.hi, a.lo + b.lo, 0 }; if (ret.lo >= BASE) { ret.hi++; ret.lo -= BASE; } if (ret.hi >= BASE) { ret.hi -= BASE; } return ret; } struct bigint sub(struct bigint a, struct bigint b) { struct bigint ret = { a.hi - b.hi, a.lo - b.lo, 0 }; if (ret.lo < 0) { ret.lo += BASE; ret.hi--; } if (ret.hi < 0) { ret.hi += BASE; } return ret; } slng mod10N(struct bigint a, int n) { if (n < 9) return a.lo % pow10[n]; else return a.lo + pow10[9] * (a.hi % pow10[n-9]); } struct bigint bigintfromslng(slng a) { struct bigint ret = {0, a, 0}; return ret; } slng slngfrombigint(struct bigint a) { return a.hi * BASE + a.lo; } // Period of Fib mod 10^n. slng fibperiod(slng n) { switch(n) { case 0: return 1; case 1: return 60; case 2: return 300; default: return (pow10[n] * 3) / 2; } } struct bigint fibn(slng n) { struct bigint a = bigintfromslng(0); struct bigint b = bigintfromslng(1); struct bigint two = bigintfromslng(2); slng m = 0; int i; for (int i = 63; i >= 0; i--) { // a = fib(m) // b = fib(m+1) // F(2m) = F(m)(2F(m+1) - F(m)) // F(2m+1) = F(m+1)^2 + F(m)^2 struct bigint aa = mul(a, sub(mul(two, b), a)); struct bigint bb = add(mul(b, b), mul(a, a)); a = aa; b = bb; m *= 2; if ((n >> i) & 1) { struct bigint cc = add(a, b); a = b; b = cc; m++; } } ASSERT(n == m); a.fib = n; return a; } vector<struct bigint> candidates[20]; int solve() { int i, j; char buf[20]; i = scanf("%s", buf); if (i != 1) return -1; N = strlen(buf); C = strtoll(buf, NULL, 10); candidates[0].clear(); candidates[0].push_back(bigintfromslng(1)); for (i = 1; i <= N; i++) { slng period = fibperiod(i); slng prevperiod = fibperiod(i-1); candidates[i].clear(); for (j = 0; j < candidates[i-1].size(); j++) { slng k = candidates[i-1][j].fib; for (k = candidates[i-1][j].fib; k < period; k += prevperiod) { struct bigint cand = fibn(k); if (mod10N(cand, i) == C % pow10[i]) { candidates[i].push_back(cand); DEBPRINT("Fib_%lld~=%lld ", cand.fib, slngfrombigint(cand)); } } } DEBPRINT("\n"); } if (candidates[N].size()) { printf("%lld\n", fibperiod(N) + candidates[N][0].fib); //printf("TAK\n"); } else { printf("NIE\n"); } return 0; } int main() { int i, j; pow10[0] = 1; for (i = 1; i <= 18; i++) pow10[i] = pow10[i-1]*10LL; #ifdef DEBUG while (!solve()) {}; #else solve(); #endif 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 | #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> using namespace std; //#define DEBUG #ifdef DEBUG #define ASSERT(x) assert(x) #define DEBPRINT(...) fprintf(stderr, __VA_ARGS__) #else #define ASSERT(x) #define DEBPRINT(...) #endif typedef long long slng; slng pow10[19]; slng C; int N; #define BASE 1000000000LL // bigint modulo 10^18, kept positive. struct bigint { slng hi, lo; // 10^9 each. slng fib; // tag it along. }; // mul mod 10^18. struct bigint mul(struct bigint a, struct bigint b) { struct bigint ret = { a.hi * b.lo + b.hi * a.lo, a.lo * b.lo, 0 }; ret.hi += ret.lo / BASE; ret.lo %= BASE; ret.hi %= BASE; return ret; } struct bigint add(struct bigint a, struct bigint b) { struct bigint ret = { a.hi + b.hi, a.lo + b.lo, 0 }; if (ret.lo >= BASE) { ret.hi++; ret.lo -= BASE; } if (ret.hi >= BASE) { ret.hi -= BASE; } return ret; } struct bigint sub(struct bigint a, struct bigint b) { struct bigint ret = { a.hi - b.hi, a.lo - b.lo, 0 }; if (ret.lo < 0) { ret.lo += BASE; ret.hi--; } if (ret.hi < 0) { ret.hi += BASE; } return ret; } slng mod10N(struct bigint a, int n) { if (n < 9) return a.lo % pow10[n]; else return a.lo + pow10[9] * (a.hi % pow10[n-9]); } struct bigint bigintfromslng(slng a) { struct bigint ret = {0, a, 0}; return ret; } slng slngfrombigint(struct bigint a) { return a.hi * BASE + a.lo; } // Period of Fib mod 10^n. slng fibperiod(slng n) { switch(n) { case 0: return 1; case 1: return 60; case 2: return 300; default: return (pow10[n] * 3) / 2; } } struct bigint fibn(slng n) { struct bigint a = bigintfromslng(0); struct bigint b = bigintfromslng(1); struct bigint two = bigintfromslng(2); slng m = 0; int i; for (int i = 63; i >= 0; i--) { // a = fib(m) // b = fib(m+1) // F(2m) = F(m)(2F(m+1) - F(m)) // F(2m+1) = F(m+1)^2 + F(m)^2 struct bigint aa = mul(a, sub(mul(two, b), a)); struct bigint bb = add(mul(b, b), mul(a, a)); a = aa; b = bb; m *= 2; if ((n >> i) & 1) { struct bigint cc = add(a, b); a = b; b = cc; m++; } } ASSERT(n == m); a.fib = n; return a; } vector<struct bigint> candidates[20]; int solve() { int i, j; char buf[20]; i = scanf("%s", buf); if (i != 1) return -1; N = strlen(buf); C = strtoll(buf, NULL, 10); candidates[0].clear(); candidates[0].push_back(bigintfromslng(1)); for (i = 1; i <= N; i++) { slng period = fibperiod(i); slng prevperiod = fibperiod(i-1); candidates[i].clear(); for (j = 0; j < candidates[i-1].size(); j++) { slng k = candidates[i-1][j].fib; for (k = candidates[i-1][j].fib; k < period; k += prevperiod) { struct bigint cand = fibn(k); if (mod10N(cand, i) == C % pow10[i]) { candidates[i].push_back(cand); DEBPRINT("Fib_%lld~=%lld ", cand.fib, slngfrombigint(cand)); } } } DEBPRINT("\n"); } if (candidates[N].size()) { printf("%lld\n", fibperiod(N) + candidates[N][0].fib); //printf("TAK\n"); } else { printf("NIE\n"); } return 0; } int main() { int i, j; pow10[0] = 1; for (i = 1; i <= 18; i++) pow10[i] = pow10[i-1]*10LL; #ifdef DEBUG while (!solve()) {}; #else solve(); #endif return 0; } |