#include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int INF = 1e9; const int64 MOD = SQ(1000LL * 1000 * 1000); int64 mulmod(int64 a, int64 b, int64 c){ if(a < b) swap(a, b); int64 r = 0; while(b){ if(b&1){ r += a; if(r >= c) r -= c; } b /= 2; a *= 2; if(a >= c) a -= c; } return r; } PLL fibp(int64 n, int64 mod){ if(n == 0) return PLL(0, 1); PLL p = fibp(n / 2, mod); // 2 * p.st * p.nd - p.st * p.st // p.nd * p.nd + p.st * p.st int64 a = mulmod(p.st, p.nd, mod); a *= 2; if(a >= mod) a -= mod; int64 b = mulmod(p.st, p.st, mod); int64 c = mulmod(p.nd, p.nd, mod); p.st = a - b; if(p.st < 0) p.st += mod; p.nd = c + b; if(p.nd >= mod) p.nd -= mod; if(n&1){ p.st += p.nd; if(p.st >= mod) p.st -= mod; swap(p.st, p.nd); } return p; } int64 fib(int64 n, int64 mod){ return fibp(n, mod).st; } int n; int64 s; int64 sm[20]; char d[23]; int getlen(int64 x){ int c = 0; while(x){ ++c; x /= 10; } return max(c, 1); } int len[84]; bool search(int64 c, int64 i, int64 m){ if(i == n){ if(c < 84 && len[c] < n) return 0; //printf("TAK\n"); printf("%lld\n", c); return 1; } FWD(r,0,10){ if(fib(c, m*10) == sm[i]) if(::search(c, i+1, m*10)) return 1; c += m*6; } return 0; } bool solve(){ scanf("%s", d); n = (int)strlen(d); sscanf(d, "%lld", &s); int64 m = 10; FWD(i,0,20){ sm[i] = s % m; m *= 10; } FWD(i,0,60) if(fib(i, 10) == sm[0]) if(::search(i, 1, 10)) return 1; printf("NIE\n"); return 1; } int main(){ FWD(i,0,84) len[i] = getlen(fib(i, MOD)); solve(); 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 | #include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int INF = 1e9; const int64 MOD = SQ(1000LL * 1000 * 1000); int64 mulmod(int64 a, int64 b, int64 c){ if(a < b) swap(a, b); int64 r = 0; while(b){ if(b&1){ r += a; if(r >= c) r -= c; } b /= 2; a *= 2; if(a >= c) a -= c; } return r; } PLL fibp(int64 n, int64 mod){ if(n == 0) return PLL(0, 1); PLL p = fibp(n / 2, mod); // 2 * p.st * p.nd - p.st * p.st // p.nd * p.nd + p.st * p.st int64 a = mulmod(p.st, p.nd, mod); a *= 2; if(a >= mod) a -= mod; int64 b = mulmod(p.st, p.st, mod); int64 c = mulmod(p.nd, p.nd, mod); p.st = a - b; if(p.st < 0) p.st += mod; p.nd = c + b; if(p.nd >= mod) p.nd -= mod; if(n&1){ p.st += p.nd; if(p.st >= mod) p.st -= mod; swap(p.st, p.nd); } return p; } int64 fib(int64 n, int64 mod){ return fibp(n, mod).st; } int n; int64 s; int64 sm[20]; char d[23]; int getlen(int64 x){ int c = 0; while(x){ ++c; x /= 10; } return max(c, 1); } int len[84]; bool search(int64 c, int64 i, int64 m){ if(i == n){ if(c < 84 && len[c] < n) return 0; //printf("TAK\n"); printf("%lld\n", c); return 1; } FWD(r,0,10){ if(fib(c, m*10) == sm[i]) if(::search(c, i+1, m*10)) return 1; c += m*6; } return 0; } bool solve(){ scanf("%s", d); n = (int)strlen(d); sscanf(d, "%lld", &s); int64 m = 10; FWD(i,0,20){ sm[i] = s % m; m *= 10; } FWD(i,0,60) if(fib(i, 10) == sm[0]) if(::search(i, 1, 10)) return 1; printf("NIE\n"); return 1; } int main(){ FWD(i,0,84) len[i] = getlen(fib(i, MOD)); solve(); return 0; } |