#include <iostream> #include <cstdio> #include <ctime> #include <algorithm> #include <vector> #include <set> using namespace std; #define ULLI unsigned long long int #define FORi for(int i = 0; i < nn; ++i) #define FORj for(int j = 0; j < nn; ++j) #define FORk for(int k = 0; k < nn; ++k) const int nn = 2; ULLI res[nn][nn]; int bili = 1000 * 1000 * 1000; const ULLI inff = (long long) bili * bili; ULLI mod = inff; ULLI pommod = 1; ULLI func(ULLI x, ULLI y) { ULLI a = x / bili; ULLI b = x % bili; ULLI c = y / bili; ULLI d = y % bili; y = b * d; x = a * d + b * c; x %= bili; x += y / bili; x %= bili; y %= bili; return x * bili + y; } void mnoz(ULLI a[nn][nn], ULLI b[nn][nn]) { FORi FORj res[i][j] = 0; FORi FORk FORj res[i][j] += func(a[i][k], b[k][j]); FORi FORj res[i][j] %= mod; } ULLI potLOG[nn][nn]; ULLI pot(ULLI k) { k--; ULLI b[nn][nn]; b[0][0] = b[1][1] = 1; b[0][1] = b[1][0] = 0; ULLI a[nn][nn]; a[0][0] = a[0][1] = a[1][0] = 1; a[1][1] = 0; while(k > 0) { if(k & 1) { mnoz(a, b); FORi FORj b[i][j] = res[i][j]; } mnoz(a, a); FORi FORj a[i][j] = res[i][j]; k /= 2; } FORi FORj potLOG[i][j] = b[i][j]; return b[0][0]; } char slo[20]; int n; int main() { ios_base::sync_with_stdio(0); long long n = 0; cin >> slo; pommod = 1; mod = inff; for(int i = 0; slo[i]; ++i) { pommod *= 10; n *= 10; n += slo[i] - '0'; } for(int i = 0; i < 20; ++i) slo[i] = 0; mod = 1; ULLI kr = 1; vector <ULLI> q; vector <ULLI> pom; q.push_back(1); do { mod *= 1000; ULLI mm = n % mod; kr = mod / 1000; kr *= 3; kr /= 2; mod = min(mod, pommod); pot(kr + 1); ULLI ep = potLOG[0][0]; ULLI fp = potLOG[0][1]; ULLI gp = potLOG[1][0]; ULLI hp = potLOG[1][1]; for(int j = 0; j < q.size(); ++j) { ULLI A = pot(q[j] + 1); ULLI B = pot(q[j]); for(ULLI i = q[j]; 2 * i <= 3 * mod; i += kr) { if(B == mm) pom.push_back(i); ULLI Ap = func(ep, A) + func(fp, B); ULLI Bp = func(gp, A) + func(hp, B); A = Ap % mod; B = Bp % mod; } } q.clear(); for(int i = 0; i < pom.size(); ++i) q.push_back(pom[i]); pom.clear(); } while(mod < pommod); kr = 3 * mod; kr /= 2; if(q.size() == 0) cout << "NIE\n"; else { ULLI wyn = q[0]; for(int i = 1; i < q.size(); ++i) wyn = min(wyn, q[i]); wyn += kr; cout << wyn << endl; } //system("pause"); 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 | #include <iostream> #include <cstdio> #include <ctime> #include <algorithm> #include <vector> #include <set> using namespace std; #define ULLI unsigned long long int #define FORi for(int i = 0; i < nn; ++i) #define FORj for(int j = 0; j < nn; ++j) #define FORk for(int k = 0; k < nn; ++k) const int nn = 2; ULLI res[nn][nn]; int bili = 1000 * 1000 * 1000; const ULLI inff = (long long) bili * bili; ULLI mod = inff; ULLI pommod = 1; ULLI func(ULLI x, ULLI y) { ULLI a = x / bili; ULLI b = x % bili; ULLI c = y / bili; ULLI d = y % bili; y = b * d; x = a * d + b * c; x %= bili; x += y / bili; x %= bili; y %= bili; return x * bili + y; } void mnoz(ULLI a[nn][nn], ULLI b[nn][nn]) { FORi FORj res[i][j] = 0; FORi FORk FORj res[i][j] += func(a[i][k], b[k][j]); FORi FORj res[i][j] %= mod; } ULLI potLOG[nn][nn]; ULLI pot(ULLI k) { k--; ULLI b[nn][nn]; b[0][0] = b[1][1] = 1; b[0][1] = b[1][0] = 0; ULLI a[nn][nn]; a[0][0] = a[0][1] = a[1][0] = 1; a[1][1] = 0; while(k > 0) { if(k & 1) { mnoz(a, b); FORi FORj b[i][j] = res[i][j]; } mnoz(a, a); FORi FORj a[i][j] = res[i][j]; k /= 2; } FORi FORj potLOG[i][j] = b[i][j]; return b[0][0]; } char slo[20]; int n; int main() { ios_base::sync_with_stdio(0); long long n = 0; cin >> slo; pommod = 1; mod = inff; for(int i = 0; slo[i]; ++i) { pommod *= 10; n *= 10; n += slo[i] - '0'; } for(int i = 0; i < 20; ++i) slo[i] = 0; mod = 1; ULLI kr = 1; vector <ULLI> q; vector <ULLI> pom; q.push_back(1); do { mod *= 1000; ULLI mm = n % mod; kr = mod / 1000; kr *= 3; kr /= 2; mod = min(mod, pommod); pot(kr + 1); ULLI ep = potLOG[0][0]; ULLI fp = potLOG[0][1]; ULLI gp = potLOG[1][0]; ULLI hp = potLOG[1][1]; for(int j = 0; j < q.size(); ++j) { ULLI A = pot(q[j] + 1); ULLI B = pot(q[j]); for(ULLI i = q[j]; 2 * i <= 3 * mod; i += kr) { if(B == mm) pom.push_back(i); ULLI Ap = func(ep, A) + func(fp, B); ULLI Bp = func(gp, A) + func(hp, B); A = Ap % mod; B = Bp % mod; } } q.clear(); for(int i = 0; i < pom.size(); ++i) q.push_back(pom[i]); pom.clear(); } while(mod < pommod); kr = 3 * mod; kr /= 2; if(q.size() == 0) cout << "NIE\n"; else { ULLI wyn = q[0]; for(int i = 1; i < q.size(); ++i) wyn = min(wyn, q[i]); wyn += kr; cout << wyn << endl; } //system("pause"); return 0; } |