#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; char s[20]; lint p10[20], p5[20], p2[20]; lint reszta; VI F2_18; VI F5; int dl; void init(){ p2[0] = p10[0] = p5[0] = 1LL; FOR(i,1,19){ p2[i] = 2LL*p2[i-1]; p5[i] = 5LL*p5[i-1]; p10[i] = 10LL*p10[i-1]; } reszta = 0; FOR(i,0,dl) { reszta*=10LL; reszta+=(s[i]-'0'); } F2_18.pb(0); F2_18.pb(1); FOR(i,2,p2[17]*3+2) { F2_18.pb((F2_18[i-1]+F2_18[i-2])%p2[18]); } F5.pb(0); F5.pb(1); FOR(i,2,20){ F5.pb((F5[i-1]+F5[i-2])%5); } } int check_power2(){ lint red = reszta%p2[dl]; int ans = -1; FOR(i,0,F2_18.size()){ if(F2_18[i]%p2[dl] == red) return i; } return -1; } lint mnoz(lint a, lint b, lint mod){ if(b==0) return 0; lint x = mnoz(a,b/2,mod); lint dwa = (x+x)%mod; if(b%2==0) return dwa; return (dwa+a)%mod; } lint MOD; struct matrix { lint A[2][2]; matrix(int x = 0){ FOR(i,0,2) FOR(j,0,2) A[i][j] = 0; FOR(i,0,2) A[i][i] = x; } friend matrix operator*( const matrix &a, const matrix &b) { matrix res(0); FOR(i,0,2) FOR(j,0,2){ lint act = 0; FOR(k,0,2){ act += mnoz(a.A[i][k],b.A[k][j],MOD); if(act>=MOD) act -= MOD; } res.A[i][j] = act%MOD; } return res; } }; matrix pwr(matrix a, lint b) { matrix res(1); for (; b; b >>= 1, a = a * a) if (b & 1) res = res * a; return res; } lint FIBMOD(lint n, lint mod){ MOD = mod; matrix A(0); A.A[0][0] = 1; A.A[0][1] = A.A[1][0] = 1; A.A[1][1] = 0; matrix B = pwr(A, n); return B.A[1][0]; } lint check_power5(int mod4){ lint red = reszta%p5[dl]; vector<lint> poss; FOR(i,0,20) if(F5[i]%5==red%5) poss.pb(i); //debugv(poss); FOR(i,2,dl+1){ vector<lint> nposs; FORE(j,poss){ lint x = *j; FOR(u,0,5) { lint FF = FIBMOD(x+u*4LL*p5[i-1], p5[i]); // debug2(x+u*4LL*p5[i-1],FF); if(FF == red%p5[i]){ nposs.pb(x+u*4LL*p5[i-1]); } } } poss = nposs; //debugv(poss); } FORE(i,poss){ if((*i)%4==mod4) return *i; } } int main(){ scanf("%s",s); dl = strlen(s); init(); // debug2(dl,reszta); int l1 = check_power2(); if(l1==-1){ printf("NIE\n"); return 0; } lint l2 = check_power5(l1%4); // debug2(l1,l2); lint mod1 = max(3LL*p2[dl-1],12LL); lint mod2 = p5[dl]; lint answer = l2; while( answer % mod1 != l1){ answer += 4LL*p5[dl]; } if(answer < 100) answer += mod1*mod2; printf("%lld\n",answer); //debug(FIBMOD(1499999999999999998LL,p10[18])); }
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 178 179 180 181 | #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; char s[20]; lint p10[20], p5[20], p2[20]; lint reszta; VI F2_18; VI F5; int dl; void init(){ p2[0] = p10[0] = p5[0] = 1LL; FOR(i,1,19){ p2[i] = 2LL*p2[i-1]; p5[i] = 5LL*p5[i-1]; p10[i] = 10LL*p10[i-1]; } reszta = 0; FOR(i,0,dl) { reszta*=10LL; reszta+=(s[i]-'0'); } F2_18.pb(0); F2_18.pb(1); FOR(i,2,p2[17]*3+2) { F2_18.pb((F2_18[i-1]+F2_18[i-2])%p2[18]); } F5.pb(0); F5.pb(1); FOR(i,2,20){ F5.pb((F5[i-1]+F5[i-2])%5); } } int check_power2(){ lint red = reszta%p2[dl]; int ans = -1; FOR(i,0,F2_18.size()){ if(F2_18[i]%p2[dl] == red) return i; } return -1; } lint mnoz(lint a, lint b, lint mod){ if(b==0) return 0; lint x = mnoz(a,b/2,mod); lint dwa = (x+x)%mod; if(b%2==0) return dwa; return (dwa+a)%mod; } lint MOD; struct matrix { lint A[2][2]; matrix(int x = 0){ FOR(i,0,2) FOR(j,0,2) A[i][j] = 0; FOR(i,0,2) A[i][i] = x; } friend matrix operator*( const matrix &a, const matrix &b) { matrix res(0); FOR(i,0,2) FOR(j,0,2){ lint act = 0; FOR(k,0,2){ act += mnoz(a.A[i][k],b.A[k][j],MOD); if(act>=MOD) act -= MOD; } res.A[i][j] = act%MOD; } return res; } }; matrix pwr(matrix a, lint b) { matrix res(1); for (; b; b >>= 1, a = a * a) if (b & 1) res = res * a; return res; } lint FIBMOD(lint n, lint mod){ MOD = mod; matrix A(0); A.A[0][0] = 1; A.A[0][1] = A.A[1][0] = 1; A.A[1][1] = 0; matrix B = pwr(A, n); return B.A[1][0]; } lint check_power5(int mod4){ lint red = reszta%p5[dl]; vector<lint> poss; FOR(i,0,20) if(F5[i]%5==red%5) poss.pb(i); //debugv(poss); FOR(i,2,dl+1){ vector<lint> nposs; FORE(j,poss){ lint x = *j; FOR(u,0,5) { lint FF = FIBMOD(x+u*4LL*p5[i-1], p5[i]); // debug2(x+u*4LL*p5[i-1],FF); if(FF == red%p5[i]){ nposs.pb(x+u*4LL*p5[i-1]); } } } poss = nposs; //debugv(poss); } FORE(i,poss){ if((*i)%4==mod4) return *i; } } int main(){ scanf("%s",s); dl = strlen(s); init(); // debug2(dl,reszta); int l1 = check_power2(); if(l1==-1){ printf("NIE\n"); return 0; } lint l2 = check_power5(l1%4); // debug2(l1,l2); lint mod1 = max(3LL*p2[dl-1],12LL); lint mod2 = p5[dl]; lint answer = l2; while( answer % mod1 != l1){ answer += 4LL*p5[dl]; } if(answer < 100) answer += mod1*mod2; printf("%lld\n",answer); //debug(FIBMOD(1499999999999999998LL,p10[18])); } |