#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef unsigned long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<ll> sll; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<ll> vll; struct Matrix { ll a, b, c, d; }; const int NMAX = 25; const ll MASK = (1LL<<32)-1; int n, iters[NMAX]; string dig; ll ten[NMAX], step[NMAX]; void shift(ll &x, int k, ll &mod) { REP(i, k) { x *= 2; if(x >= mod) x -= mod; } } ll mul(ll &a, ll &b, ll &mod) { uint al = (a & MASK), bl = (b & MASK); uint ah = (a >> 32), bh = (b >> 32); ll ans = ((ll) al * bl) % mod; ll tmp = ((ll) ah * bh) % mod; shift(tmp, 64, mod); ans = (ans + tmp) % mod; tmp = ((ll) ah * bl) % mod; shift(tmp, 32, mod); ans = (ans + tmp) % mod; tmp = ((ll) al * bh) % mod; shift(tmp, 32, mod); ans = (ans + tmp) % mod; //dbg cout << a << " * " << b << " = " << ans << endl; //dbg cout << ((1ULL<<32)*ah + al) << ", " << ((1ULL<<32)*bh + bl) << endl; return ans; } Matrix oneMat() { Matrix one; one.a = one.b = one.c = 1; one.d = 0; return one; } Matrix mulMat(Matrix m1, Matrix m2, ll &mod) { Matrix ans; ans.a = (mul(m1.a, m2.a, mod) + mul(m1.b, m2.c, mod)) % mod; ans.b = (mul(m1.a, m2.b, mod) + mul(m1.b, m2.d, mod)) % mod; ans.c = (mul(m1.c, m2.a, mod) + mul(m1.d, m2.c, mod)) % mod; ans.d = (mul(m1.c, m2.b, mod) + mul(m1.d, m2.d, mod)) % mod; return ans; } Matrix getMat(ll x, ll &mod) { if(x == 1) return oneMat(); Matrix tmp = getMat(x/2, mod); tmp = mulMat(tmp, tmp, mod); if(x % 2) tmp = mulMat(tmp, oneMat(), mod); return tmp; } inline ll getFib(ll x, ll &mod) { if(x == 0ULL) return 0ULL; return getMat(x, mod).c; } vll brutForceIndices(ll x, int digits) { sll tmp; if(x == 0) tmp.insert(0); if(x == 1) tmp.insert(1); ll a = 0, b = 1; FORE(i, 2, step[digits]) { ll c = b; b = (a + b) % ten[digits]; a = c; if(b == x) tmp.insert(i); } return vll(ALL(tmp)); } vll getIndices(ll x, int digits) { dbg cout << "getIndices: " << x << " " << ten[digits] << endl; if(digits <= 3) return brutForceIndices(x, digits); vll smaller = getIndices(x % ten[digits - 1], digits - 1); vll ans; FORIT(it, smaller, vll) { ll ind = *it; REP(i, iters[digits]) { if(getFib(ind, ten[digits]) == x) { ans.pb(ind); dbg cout << "found on level " << digits << ": " << ind << endl; } ind += step[digits - 1]; } } return ans; } int main() { ten[1] = 10; ten[2] = 100; ten[3] = 1000; step[1] = 60; step[2] = 300; step[3] = 1500; iters[2] = 5; iters[3] = 5; FORE(i, 4, NMAX - 1) { step[i] = step[i-1] * 10; ten[i] = ten[i-1] * 10; iters[i] = 10; } cin >> dig; n = dig.size(); ll x = 0; REP(i, n) x = x * 10 + (dig[i] - '0'); vll out = getIndices(x, dig.size()); /*FORIT(it, out, vll) cout << *it << endl;*/ if(out.empty()) printf("NIE"); else { ll ret = out[0] + step[18]; cout << ret;// << " " << getFib(ret, ten[18]) << 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 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef unsigned long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<ll> sll; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<ll> vll; struct Matrix { ll a, b, c, d; }; const int NMAX = 25; const ll MASK = (1LL<<32)-1; int n, iters[NMAX]; string dig; ll ten[NMAX], step[NMAX]; void shift(ll &x, int k, ll &mod) { REP(i, k) { x *= 2; if(x >= mod) x -= mod; } } ll mul(ll &a, ll &b, ll &mod) { uint al = (a & MASK), bl = (b & MASK); uint ah = (a >> 32), bh = (b >> 32); ll ans = ((ll) al * bl) % mod; ll tmp = ((ll) ah * bh) % mod; shift(tmp, 64, mod); ans = (ans + tmp) % mod; tmp = ((ll) ah * bl) % mod; shift(tmp, 32, mod); ans = (ans + tmp) % mod; tmp = ((ll) al * bh) % mod; shift(tmp, 32, mod); ans = (ans + tmp) % mod; //dbg cout << a << " * " << b << " = " << ans << endl; //dbg cout << ((1ULL<<32)*ah + al) << ", " << ((1ULL<<32)*bh + bl) << endl; return ans; } Matrix oneMat() { Matrix one; one.a = one.b = one.c = 1; one.d = 0; return one; } Matrix mulMat(Matrix m1, Matrix m2, ll &mod) { Matrix ans; ans.a = (mul(m1.a, m2.a, mod) + mul(m1.b, m2.c, mod)) % mod; ans.b = (mul(m1.a, m2.b, mod) + mul(m1.b, m2.d, mod)) % mod; ans.c = (mul(m1.c, m2.a, mod) + mul(m1.d, m2.c, mod)) % mod; ans.d = (mul(m1.c, m2.b, mod) + mul(m1.d, m2.d, mod)) % mod; return ans; } Matrix getMat(ll x, ll &mod) { if(x == 1) return oneMat(); Matrix tmp = getMat(x/2, mod); tmp = mulMat(tmp, tmp, mod); if(x % 2) tmp = mulMat(tmp, oneMat(), mod); return tmp; } inline ll getFib(ll x, ll &mod) { if(x == 0ULL) return 0ULL; return getMat(x, mod).c; } vll brutForceIndices(ll x, int digits) { sll tmp; if(x == 0) tmp.insert(0); if(x == 1) tmp.insert(1); ll a = 0, b = 1; FORE(i, 2, step[digits]) { ll c = b; b = (a + b) % ten[digits]; a = c; if(b == x) tmp.insert(i); } return vll(ALL(tmp)); } vll getIndices(ll x, int digits) { dbg cout << "getIndices: " << x << " " << ten[digits] << endl; if(digits <= 3) return brutForceIndices(x, digits); vll smaller = getIndices(x % ten[digits - 1], digits - 1); vll ans; FORIT(it, smaller, vll) { ll ind = *it; REP(i, iters[digits]) { if(getFib(ind, ten[digits]) == x) { ans.pb(ind); dbg cout << "found on level " << digits << ": " << ind << endl; } ind += step[digits - 1]; } } return ans; } int main() { ten[1] = 10; ten[2] = 100; ten[3] = 1000; step[1] = 60; step[2] = 300; step[3] = 1500; iters[2] = 5; iters[3] = 5; FORE(i, 4, NMAX - 1) { step[i] = step[i-1] * 10; ten[i] = ten[i-1] * 10; iters[i] = 10; } cin >> dig; n = dig.size(); ll x = 0; REP(i, n) x = x * 10 + (dig[i] - '0'); vll out = getIndices(x, dig.size()); /*FORIT(it, out, vll) cout << *it << endl;*/ if(out.empty()) printf("NIE"); else { ll ret = out[0] + step[18]; cout << ret;// << " " << getFib(ret, ten[18]) << endl; } return 0; } |