//Daniel Grzegorzewski #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define ST first #define ND second #define int unsigned long long using namespace std; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } const int M = (int)1e18; int fi[2010], in, wyn, pot[20]; map<int, int> F; string a; bool flag, bla; int f(int n) { if (F.count(n)) return F[n]; int k = n/2; if (n%2 == 0) { int p = f(k)/pot[9]; int q = f(k)%pot[9]; int r = f(k-1)/pot[9]; int s = f(k-1)%pot[9]; return F[n] = (pot[9]*((2*p*q)%pot[9])+q*q + 2*(pot[9]*((p*s+q*r)%pot[9])+q*s))%M; } else { int p = f(k+1)/pot[9]; int q = f(k+1)%pot[9]; int r = f(k)/pot[9]; int s = f(k)%pot[9]; return F[n] = (pot[9]*((2*p*q)%pot[9])+q*q + pot[9]*((2*r*s)%pot[9])+s*s)%M; } } int cyfry(int x) { int res = 0; while (x) { x /= 10; ++res; } return res; } void licz(int x, int ile) { if (flag) return; if (ile == a.size() && cyfry(f(x)) >= a.size()) { wyn = x; flag = true; return; } if (ile == a.size()) return; int co = 15*pot[ile-1]; int gr = 15*pot[ile]; for (int i = x; !flag && i <= gr; i += co) if (f(i)%pot[ile+1] == in%pot[ile+1]) licz(i, ile+1); } #undef int int main() { #define int unsigned long long init_ios(); cin >> a; int co = 1; pot[0] = 1; for (int i = 1; i <= 18; ++i) pot[i] = 10*pot[i-1]; for (int i = a.size()-1; i >= 0; --i) { in += co*(int)(a[i]-'0'); co *= 10; if (i == 0) break; } if (in == 0) bla = true; fi[1] = fi[2] = 1; F[0] = 0; F[1] = 1; for (int i = 3; i < 2000; ++i) fi[i] = (fi[i-1]+fi[i-2])%M; if (a.size() <= 3) { for (int i = 0; i < 1500; ++i) if (fi[i]%co == in && cyfry(fi[i]) >= a.size()) { cout<<i<<"\n"; return 0; } cout<<"NIE\n"; return 0; } for (int i = 0; !flag && i < 1500; ++i) if (fi[i]%1000 == in%1000) licz(i, 3); if (!flag) { cout<<"NIE\n"; return 0; } cout<<wyn<<"\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 | //Daniel Grzegorzewski #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define ST first #define ND second #define int unsigned long long using namespace std; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } const int M = (int)1e18; int fi[2010], in, wyn, pot[20]; map<int, int> F; string a; bool flag, bla; int f(int n) { if (F.count(n)) return F[n]; int k = n/2; if (n%2 == 0) { int p = f(k)/pot[9]; int q = f(k)%pot[9]; int r = f(k-1)/pot[9]; int s = f(k-1)%pot[9]; return F[n] = (pot[9]*((2*p*q)%pot[9])+q*q + 2*(pot[9]*((p*s+q*r)%pot[9])+q*s))%M; } else { int p = f(k+1)/pot[9]; int q = f(k+1)%pot[9]; int r = f(k)/pot[9]; int s = f(k)%pot[9]; return F[n] = (pot[9]*((2*p*q)%pot[9])+q*q + pot[9]*((2*r*s)%pot[9])+s*s)%M; } } int cyfry(int x) { int res = 0; while (x) { x /= 10; ++res; } return res; } void licz(int x, int ile) { if (flag) return; if (ile == a.size() && cyfry(f(x)) >= a.size()) { wyn = x; flag = true; return; } if (ile == a.size()) return; int co = 15*pot[ile-1]; int gr = 15*pot[ile]; for (int i = x; !flag && i <= gr; i += co) if (f(i)%pot[ile+1] == in%pot[ile+1]) licz(i, ile+1); } #undef int int main() { #define int unsigned long long init_ios(); cin >> a; int co = 1; pot[0] = 1; for (int i = 1; i <= 18; ++i) pot[i] = 10*pot[i-1]; for (int i = a.size()-1; i >= 0; --i) { in += co*(int)(a[i]-'0'); co *= 10; if (i == 0) break; } if (in == 0) bla = true; fi[1] = fi[2] = 1; F[0] = 0; F[1] = 1; for (int i = 3; i < 2000; ++i) fi[i] = (fi[i-1]+fi[i-2])%M; if (a.size() <= 3) { for (int i = 0; i < 1500; ++i) if (fi[i]%co == in && cyfry(fi[i]) >= a.size()) { cout<<i<<"\n"; return 0; } cout<<"NIE\n"; return 0; } for (int i = 0; !flag && i < 1500; ++i) if (fi[i]%1000 == in%1000) licz(i, 3); if (!flag) { cout<<"NIE\n"; return 0; } cout<<wyn<<"\n"; } |