// PA 2015, miodziu@poczta.fm #include <cstdio> #include <cstdlib> #include <vector> #include <string> #include <algorithm> #include <queue> #include <set> #include <map> #include <ctime> #include <cmath> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef long long LL; typedef vector<LL> VLL; typedef unsigned long long ULL; #define mp make_pair int read() { int n; scanf("%d", &n); return n; } char readc() { char c; scanf("%c", &c); return c; } LL readl() { LL n; scanf("%lld", &n); return n; } #define REP(i,n) for (int i=0,_=(n); i<_; ++i) #define FOR(i,a,b) for (int i=(a),_=(b); i<=_; ++i) #define FORD(i,a,b) for (int i=(a),_=(b); i>=_; --i) #define st first #define nd second LL p; inline LL add(LL a, LL b, LL mod) { a += b; if (a >= mod) a -= mod; return a; } inline LL mul(LL a, LL b, LL mod) { LL res = 0; while (a > 0) { if (a % 2 == 1) res = add(res, b, mod); b = add(b, b, mod); a /= 2; } return res; } inline LL mix(LL a, LL b, LL c, LL d, LL mod) { return add(mul(a, b, mod), mul(c, d, mod), mod); } struct FibMx { LL a, b, c; FibMx(int n) : a(1), b(n), c(1-n) {} FibMx &operator *=(const FibMx &oth) { LL aa = mix(a, oth.a, b, oth.b, p); LL bb = mix(b, oth.a, c, oth.b, p); LL cc = mix(b, oth.b, c, oth.c, p); a = aa; b = bb; c = cc; return *this; } }; void fib(LL n, LL &a, LL &b) { FibMx res(0); FibMx x(1); while (n > 0) { if (n % 2 == 1) res *= x; x *= x; n /= 2; } a = res.a; b = res.b; } LL fib(LL n) { LL res, tmp; fib(n, tmp, res); return res; } LL solve(char *s, int n) { if (n < 4) { int r = 0; REP(i, n) r = 10 * r + s[i] - '0'; p = 1; REP(i, n) p *= 10; int a = -1, b = 1; REP(i, 1500) { int tt = a + b; if (tt >= p) tt -= p; a = b; b = tt; if (b == r) { if (n == 1) i += 60; if (n == 2) i += 300; if (n == 3) i += 1500; return i; } } return -1; } vector<LL> v; LL r = 100 * (s[n-3] - '0') + 10 * (s[n-2] - '0') + (s[n-1] - '0'); int a = -1, b = 1; p = 1000; REP(i, 1500) { int tt = a + b; if (tt >= p) tt -= p; a = b; b = tt; if (b == r) { v.push_back(i); } } FORD(x, n-4, 0) { LL step = p + p/2; r += p * (s[x] - '0'); p *= 10; REP(i, v.size()) FOR(k, 1, 9) v.push_back(v[i] + step * k); vector<LL> vv; REP(i, v.size()) { LL f = fib(v[i]); if (f % p == r) vv.push_back(v[i]); } std::swap(v, vv); if (v.empty()) return -1; } return v.back() + p + p/2; } int main() { char s[32]; scanf("%s", s); int n = 0; while (s[n]) n++; LL res = solve(s, n); if (res < 0) printf("NIE\n"); else { printf("%lld\n", res); //printf("%lld\n", fib(res)); } 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 | // PA 2015, miodziu@poczta.fm #include <cstdio> #include <cstdlib> #include <vector> #include <string> #include <algorithm> #include <queue> #include <set> #include <map> #include <ctime> #include <cmath> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef long long LL; typedef vector<LL> VLL; typedef unsigned long long ULL; #define mp make_pair int read() { int n; scanf("%d", &n); return n; } char readc() { char c; scanf("%c", &c); return c; } LL readl() { LL n; scanf("%lld", &n); return n; } #define REP(i,n) for (int i=0,_=(n); i<_; ++i) #define FOR(i,a,b) for (int i=(a),_=(b); i<=_; ++i) #define FORD(i,a,b) for (int i=(a),_=(b); i>=_; --i) #define st first #define nd second LL p; inline LL add(LL a, LL b, LL mod) { a += b; if (a >= mod) a -= mod; return a; } inline LL mul(LL a, LL b, LL mod) { LL res = 0; while (a > 0) { if (a % 2 == 1) res = add(res, b, mod); b = add(b, b, mod); a /= 2; } return res; } inline LL mix(LL a, LL b, LL c, LL d, LL mod) { return add(mul(a, b, mod), mul(c, d, mod), mod); } struct FibMx { LL a, b, c; FibMx(int n) : a(1), b(n), c(1-n) {} FibMx &operator *=(const FibMx &oth) { LL aa = mix(a, oth.a, b, oth.b, p); LL bb = mix(b, oth.a, c, oth.b, p); LL cc = mix(b, oth.b, c, oth.c, p); a = aa; b = bb; c = cc; return *this; } }; void fib(LL n, LL &a, LL &b) { FibMx res(0); FibMx x(1); while (n > 0) { if (n % 2 == 1) res *= x; x *= x; n /= 2; } a = res.a; b = res.b; } LL fib(LL n) { LL res, tmp; fib(n, tmp, res); return res; } LL solve(char *s, int n) { if (n < 4) { int r = 0; REP(i, n) r = 10 * r + s[i] - '0'; p = 1; REP(i, n) p *= 10; int a = -1, b = 1; REP(i, 1500) { int tt = a + b; if (tt >= p) tt -= p; a = b; b = tt; if (b == r) { if (n == 1) i += 60; if (n == 2) i += 300; if (n == 3) i += 1500; return i; } } return -1; } vector<LL> v; LL r = 100 * (s[n-3] - '0') + 10 * (s[n-2] - '0') + (s[n-1] - '0'); int a = -1, b = 1; p = 1000; REP(i, 1500) { int tt = a + b; if (tt >= p) tt -= p; a = b; b = tt; if (b == r) { v.push_back(i); } } FORD(x, n-4, 0) { LL step = p + p/2; r += p * (s[x] - '0'); p *= 10; REP(i, v.size()) FOR(k, 1, 9) v.push_back(v[i] + step * k); vector<LL> vv; REP(i, v.size()) { LL f = fib(v[i]); if (f % p == r) vv.push_back(v[i]); } std::swap(v, vv); if (v.empty()) return -1; } return v.back() + p + p/2; } int main() { char s[32]; scanf("%s", s); int n = 0; while (s[n]) n++; LL res = solve(s, n); if (res < 0) printf("NIE\n"); else { printf("%lld\n", res); //printf("%lld\n", fib(res)); } return 0; } |