#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=(a);i<=(b);++i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define SORT(X) sort(X.begin(),X.end()) #define fi first #define se second long long MOD = 1000000000000000000LL; const long long MOD2 = 1000000000; struct myLL{ long long x; myLL operator *(const myLL &A) const { long long a1 = x/MOD2,b1 = x%MOD2; long long a2 = A.x/MOD2,b2 = A.x%MOD2; return myLL( ((a1*b2+b1*a2)%MOD2)*MOD2 + b1*b2); } void operator +=(const myLL &A){ x += A.x; if(x > MOD) x -= MOD; } myLL(long long a){ x = a%MOD; } }; typedef vector<vector<myLL> > matrix; matrix operator * (const matrix &A, const matrix &B) { matrix C(SZ(A), vector<myLL>(SZ(B[0]), 0)); FOR(i,SZ(A)) FOR(j,SZ(B[0])) FOR(k,SZ(B)) { C[i][j] += A[i][k] * B[k][j]; } return C; } matrix operator ^ (const matrix &A, long long x) { matrix R(SZ(A), vector<myLL>(SZ(A), 0)); matrix T = A; FOR(i,SZ(R)) R[i][i] = 1; while(x != 0) { if(x%2 == 1) R = R*T; T = T*T; x /= 2; } return R; } long long zap; int pos[100000]; matrix Base; void preparePOS(){ int a = 1, b = 2; pos[1] = 1; while(a != 1 || b != 1){ pos[b] = 1; int c = (a+b)%10000; a = b; b = c; } Base = matrix(2,vector<myLL>(2,1)); Base[0][0] = 0; } long long FIB(long long x){ matrix A = Base; return (A^x)[1][1].x; } bool wyp; bool rek(long long cmod, long long cadd, long long x){ FOR(i,13){ //printf("\n %lld %lld %lld %lld %d %d ", FIB(x), cmod, MOD, zap, FIB(x)%cmod == zap%cmod, cmod != MOD); if(FIB(x) % cmod == zap % cmod){ // printf("Tut\n"); if(cmod != MOD){ if(rek(cmod*10,cadd*10,x)) return true; } else{ printf("%lld", x+1); //printf(" | %lld |", FIB(x)); printf("\n"); wyp = true; return true; } } x += cadd; } return false; } int main () { preparePOS(); //return 0; //int zzz; //scanf("%d",&zzz); //while(zzz--){ string inp; cin>>inp; zap=atoll(inp.c_str()); MOD = 1; FOR(i,inp.size()){ MOD*=10; } wyp = false; if(MOD > 10000){ long long cmod = 10000; long long cadd = 1500; FOR(i,15010){ // if(FIB(i)%cmod == zap%cmod) printf("+"); if(FIB(i)%cmod == zap%cmod && rek(cmod*10,cadd*10,i) )break; } //printf("\n"); if(!wyp) printf("NIE\n"); } else{ FOR(j,15010){ int i = j+15000000; if(FIB(i)%MOD == zap) { printf("%d\n", i+1); wyp=true; break;} } if(!wyp)printf("NIE\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 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=(a);i<=(b);++i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define SORT(X) sort(X.begin(),X.end()) #define fi first #define se second long long MOD = 1000000000000000000LL; const long long MOD2 = 1000000000; struct myLL{ long long x; myLL operator *(const myLL &A) const { long long a1 = x/MOD2,b1 = x%MOD2; long long a2 = A.x/MOD2,b2 = A.x%MOD2; return myLL( ((a1*b2+b1*a2)%MOD2)*MOD2 + b1*b2); } void operator +=(const myLL &A){ x += A.x; if(x > MOD) x -= MOD; } myLL(long long a){ x = a%MOD; } }; typedef vector<vector<myLL> > matrix; matrix operator * (const matrix &A, const matrix &B) { matrix C(SZ(A), vector<myLL>(SZ(B[0]), 0)); FOR(i,SZ(A)) FOR(j,SZ(B[0])) FOR(k,SZ(B)) { C[i][j] += A[i][k] * B[k][j]; } return C; } matrix operator ^ (const matrix &A, long long x) { matrix R(SZ(A), vector<myLL>(SZ(A), 0)); matrix T = A; FOR(i,SZ(R)) R[i][i] = 1; while(x != 0) { if(x%2 == 1) R = R*T; T = T*T; x /= 2; } return R; } long long zap; int pos[100000]; matrix Base; void preparePOS(){ int a = 1, b = 2; pos[1] = 1; while(a != 1 || b != 1){ pos[b] = 1; int c = (a+b)%10000; a = b; b = c; } Base = matrix(2,vector<myLL>(2,1)); Base[0][0] = 0; } long long FIB(long long x){ matrix A = Base; return (A^x)[1][1].x; } bool wyp; bool rek(long long cmod, long long cadd, long long x){ FOR(i,13){ //printf("\n %lld %lld %lld %lld %d %d ", FIB(x), cmod, MOD, zap, FIB(x)%cmod == zap%cmod, cmod != MOD); if(FIB(x) % cmod == zap % cmod){ // printf("Tut\n"); if(cmod != MOD){ if(rek(cmod*10,cadd*10,x)) return true; } else{ printf("%lld", x+1); //printf(" | %lld |", FIB(x)); printf("\n"); wyp = true; return true; } } x += cadd; } return false; } int main () { preparePOS(); //return 0; //int zzz; //scanf("%d",&zzz); //while(zzz--){ string inp; cin>>inp; zap=atoll(inp.c_str()); MOD = 1; FOR(i,inp.size()){ MOD*=10; } wyp = false; if(MOD > 10000){ long long cmod = 10000; long long cadd = 1500; FOR(i,15010){ // if(FIB(i)%cmod == zap%cmod) printf("+"); if(FIB(i)%cmod == zap%cmod && rek(cmod*10,cadd*10,i) )break; } //printf("\n"); if(!wyp) printf("NIE\n"); } else{ FOR(j,15010){ int i = j+15000000; if(FIB(i)%MOD == zap) { printf("%d\n", i+1); wyp=true; break;} } if(!wyp)printf("NIE\n"); //} } } |