//Aleksander Łukasiewicz #include<bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define mp make_pair #define ALL(G) (G).begin(),(G).end() typedef long long LL; typedef pair<int,int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; typedef pair< PLL, PLL > matrix; const int INF = 1000000009; vector<LL> cand[2]; int t[] = {0, 0, 7, 12, 17, 21, 26, 31}; void brut(LL r, LL MOD, LL N, int dig){ LL fib1 = 0, fib2 = 1; if(r == 0 && 0 >= t[dig]) cand[0].push_back(0); if(r == 1 && 1 >= t[dig]) cand[0].push_back(1); for(int i=2; i<N; i++){ LL tmp = fib2; fib2 = fib1 + fib2; if(fib2 > MOD) fib2 -= MOD; fib1 = tmp; if(fib2 == r && i >= t[dig]) cand[0].push_back(i); } } LL multMod(LL a, LL b, LL MOD) { LL m,w; w = 0; m = 1; while(m) { if(b & m) w = (w + a) % MOD; a = (a << 1) % MOD; m <<= 1; } return w; } matrix mult(matrix a, matrix b, LL MOD){ matrix ret; ret.x.x = multMod(a.x.x, b.x.x, MOD) + multMod(a.x.y, b.y.x, MOD); ret.x.y = multMod(a.x.x, b.x.y, MOD) + multMod(a.x.y, b.y.y, MOD); ret.y.x = multMod(a.y.x, b.x.x, MOD) + multMod(a.y.y, b.y.x, MOD); ret.y.y = multMod(a.y.x, b.x.y, MOD) + multMod(a.y.y, b.y.y, MOD); if(ret.x.x > MOD) ret.x.x -= MOD; if(ret.x.y > MOD) ret.x.y -= MOD; if(ret.y.x > MOD) ret.y.x -= MOD; if(ret.y.y > MOD) ret.y.y -= MOD; return ret; } LL Fibo(LL k, LL MOD){ matrix a = mp(mp(1, 1), mp(1,0)); matrix ret = mp(mp(1,0), mp(0,1)); while(k){ if(k&1) ret = mult(ret, a, MOD); a = mult(a,a, MOD); k/=2; } return ret.x.y; } vector<LL> temp; void Clean(int ind){ if(cand[ind].size() == 0) return; sort(cand[ind].begin(), cand[ind].end()); temp.clear(); temp.push_back(cand[ind][0]); for(int i=1; i<(int)cand[ind].size(); i++) if(cand[ind][i] != cand[ind][i-1]) temp.push_back(cand[ind][i]); cand[ind].clear(); for(int i=0; i<(int)temp.size(); i++) cand[ind].push_back(temp[i]); } int main(){ string read; cin>>read; LL r = 0; for(int i=0; i<(int)read.size(); i++) r = 10*r + read[i] - '0'; if((int)read.size() <= 7){ int MOD = 1; for(int i=0; i<(int)read.size(); i++) MOD*=10; int N; if(MOD == 10) N = 60; else if(MOD == 100) N = 300; else N = MOD + MOD/2; brut(r, MOD, N, (int)read.size()); if(cand[0].size() == 0) cout<<"NIE\n"; else cout<<cand[0][0]<<'\n'; } else{ LL MOD = 10000000; LL N = 15000000; brut(r%MOD, MOD, N, 7); int e = 0, f = 1; for(int dig=8; dig<=(int)read.size(); dig++){ MOD*=10; N*=10; LL tmp = r%MOD; for(int i=0; i<(int)cand[e].size(); i++) for(int j=0; j<9; j++) if(Fibo(N*i + cand[e][i], MOD) == tmp) cand[f].push_back(N*i + cand[e][i]); Clean(f); swap(e,f); } if(cand[e].size() == 0) cout<<"NIE\n"; else cout<<cand[e][0]<<'\n'; } 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 | //Aleksander Łukasiewicz #include<bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define mp make_pair #define ALL(G) (G).begin(),(G).end() typedef long long LL; typedef pair<int,int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; typedef pair< PLL, PLL > matrix; const int INF = 1000000009; vector<LL> cand[2]; int t[] = {0, 0, 7, 12, 17, 21, 26, 31}; void brut(LL r, LL MOD, LL N, int dig){ LL fib1 = 0, fib2 = 1; if(r == 0 && 0 >= t[dig]) cand[0].push_back(0); if(r == 1 && 1 >= t[dig]) cand[0].push_back(1); for(int i=2; i<N; i++){ LL tmp = fib2; fib2 = fib1 + fib2; if(fib2 > MOD) fib2 -= MOD; fib1 = tmp; if(fib2 == r && i >= t[dig]) cand[0].push_back(i); } } LL multMod(LL a, LL b, LL MOD) { LL m,w; w = 0; m = 1; while(m) { if(b & m) w = (w + a) % MOD; a = (a << 1) % MOD; m <<= 1; } return w; } matrix mult(matrix a, matrix b, LL MOD){ matrix ret; ret.x.x = multMod(a.x.x, b.x.x, MOD) + multMod(a.x.y, b.y.x, MOD); ret.x.y = multMod(a.x.x, b.x.y, MOD) + multMod(a.x.y, b.y.y, MOD); ret.y.x = multMod(a.y.x, b.x.x, MOD) + multMod(a.y.y, b.y.x, MOD); ret.y.y = multMod(a.y.x, b.x.y, MOD) + multMod(a.y.y, b.y.y, MOD); if(ret.x.x > MOD) ret.x.x -= MOD; if(ret.x.y > MOD) ret.x.y -= MOD; if(ret.y.x > MOD) ret.y.x -= MOD; if(ret.y.y > MOD) ret.y.y -= MOD; return ret; } LL Fibo(LL k, LL MOD){ matrix a = mp(mp(1, 1), mp(1,0)); matrix ret = mp(mp(1,0), mp(0,1)); while(k){ if(k&1) ret = mult(ret, a, MOD); a = mult(a,a, MOD); k/=2; } return ret.x.y; } vector<LL> temp; void Clean(int ind){ if(cand[ind].size() == 0) return; sort(cand[ind].begin(), cand[ind].end()); temp.clear(); temp.push_back(cand[ind][0]); for(int i=1; i<(int)cand[ind].size(); i++) if(cand[ind][i] != cand[ind][i-1]) temp.push_back(cand[ind][i]); cand[ind].clear(); for(int i=0; i<(int)temp.size(); i++) cand[ind].push_back(temp[i]); } int main(){ string read; cin>>read; LL r = 0; for(int i=0; i<(int)read.size(); i++) r = 10*r + read[i] - '0'; if((int)read.size() <= 7){ int MOD = 1; for(int i=0; i<(int)read.size(); i++) MOD*=10; int N; if(MOD == 10) N = 60; else if(MOD == 100) N = 300; else N = MOD + MOD/2; brut(r, MOD, N, (int)read.size()); if(cand[0].size() == 0) cout<<"NIE\n"; else cout<<cand[0][0]<<'\n'; } else{ LL MOD = 10000000; LL N = 15000000; brut(r%MOD, MOD, N, 7); int e = 0, f = 1; for(int dig=8; dig<=(int)read.size(); dig++){ MOD*=10; N*=10; LL tmp = r%MOD; for(int i=0; i<(int)cand[e].size(); i++) for(int j=0; j<9; j++) if(Fibo(N*i + cand[e][i], MOD) == tmp) cand[f].push_back(N*i + cand[e][i]); Clean(f); swap(e,f); } if(cand[e].size() == 0) cout<<"NIE\n"; else cout<<cand[e][0]<<'\n'; } return 0; } |