//Tadrion #include <cstdio> #include <vector> #include <iostream> #include <deque> #include <map> #include <set> #include <string> #include <cstring> #include <cstdlib> #include <cstdint> #include <cmath> #include <queue> #include <stack> #include <algorithm> #include <utility> using namespace std; #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define CLEAR(x) (memset(x,0,sizeof(x))) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define VAR(v, n) __typeof(n) v = (n) #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define DBG(v) cout<<#v<<" = "<<v<<endl; #define IN(x,y) ((y).find(x)!=(y).end()) #define ST first #define ND second #define PB push_back #define PF push_front #define MP make_pair typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const long long RRR = 1000000000000000000LL; struct Matrix { long long a[2][2]; public: Matrix() {} Matrix(LL b) { if(b==1) { a[0][0] = 1; a[0][1]=1; a[1][0]=1; a[1][1]=0; } else if(b==0) { a[0][0] = 1; a[1][1]=1; a[0][1] = 0; a[1][0]=0; } } Matrix multMod(Matrix, long long); }; unsigned long long multLongMod(unsigned long long a, unsigned long long b, long long M) { int cur = 0; long long res = 0; while(b > 0) { LL c = b%10; //assert(b>0); b/=10; unsigned long long cc = c*a; FOR(i,1,cur) { cc *= 10; cc %= RRR; } res = (res + cc)%RRR; cur++; } return res%M; } Matrix Matrix::multMod(Matrix b, long long M) { Matrix temp(0); //FOR(i,0,1) FOR(j,0,1) temp.a[i][j] = ((a[i][0]*b.a[0][j])%M + (a[i][1]*b.a[1][j])%M)%M; FOR(i,0,1) FOR(j,0,1) temp.a[i][j] = (multLongMod(a[i][0],b.a[0][j],M) + multLongMod(a[i][1],b.a[1][j],M))%M; return temp; } Matrix powMod(LL n, LL M) { Matrix temp(0); if(n==0) return temp; temp = powMod(n/2,M); temp = temp.multMod(temp,M); if(n%2 == 0) return temp; else { Matrix temp2(1); temp = temp.multMod(temp2,M); return temp; } } unsigned long long fibMod(LL k, LL M) { Matrix temp(0); temp = powMod(k+1,M); return temp.a[1][1]; } unsigned long long a[50]; unsigned long long poww10(LL k) { long long r = 1; FOR(i,1,k) r*=10; return r; } string s; int main() { //FOR(i,1,1000) { // cout << fibMod(i,100) << "\n"; //} a[0]=1; a[1]=60; /*for(LL r=2; r <= 18; r++) { for(LL k=1; k<=100; k++) { if(fibMod(a[r-1]*k,poww10(r)) == 0 && fibMod(a[r-1]*k+1,poww10(r)) == 1) { a[r] = a[r-1]*k; cout << r << " " << a[r] << "\n"; fflush(stdout); break; } } } */ a[2]=300; a[3]=1500; unsigned long long cur = 0; FOR(i,4,18) a[i]=10LL*a[i-1]; cin >> s; //cout << SZ(s) << "\n"; FOR(j,0,SZ(s)-1) { cur = 10*cur + (s[j]-'0'); } //cout << cur << "\n"; if(cur < 10000) { int ok = 0; FOR(i,60,15061) { if(fibMod(i,poww10(SZ(s))) == cur) { cout << i << "\n"; ok=1; break; } } if(ok==0) cout << "NIE\n" << "\n"; } else { //printf("tu"); //cout << cur << "\n"; set<unsigned long long> kand; FOR(i,60,30061) { if(fibMod(i,poww10(4LL)) == cur%10000) { kand.insert(i); //cout << i << "\n"; } } int ind = 5; while(!kand.empty()) { if(poww10(ind) > cur) break; set< unsigned long long> newkand; FOREACH(it,kand) { for(unsigned long long i=0; i <= 10; i++) if(fibMod((*it) + (i*a[ind-1]),poww10(ind)) == (cur%poww10(ind))) { newkand.insert(((*it) + i*a[ind-1])%poww10(ind)); //cout << ind << " " <<((*it) + i*a[ind-1])%poww10(ind) << "\n"; } } ind++; kand = newkand; } if(kand.empty()) cout << "NIE\n"; else cout << *(kand.begin()) << "\n"; } /* unsigned long long kk = 0; FORD(i,SZ(s)-1,0) { unsigned long long cur = 0; FOR(j,i,SZ(s)-1) { cur = 10*cur + (s[j]-'0'); } int lenn = SZ(s)-i; while(fibMod(kk,poww10(lenn))!=cur) { kk += a[lenn-1]; } cout << cur << " " << kk << "\n"; fflush(stdout); } cout << kk << "\n"; */ //cout << fibMod(60,100) << "\n"; //cout << fibMod(61,100) << "\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 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | //Tadrion #include <cstdio> #include <vector> #include <iostream> #include <deque> #include <map> #include <set> #include <string> #include <cstring> #include <cstdlib> #include <cstdint> #include <cmath> #include <queue> #include <stack> #include <algorithm> #include <utility> using namespace std; #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define CLEAR(x) (memset(x,0,sizeof(x))) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define VAR(v, n) __typeof(n) v = (n) #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define DBG(v) cout<<#v<<" = "<<v<<endl; #define IN(x,y) ((y).find(x)!=(y).end()) #define ST first #define ND second #define PB push_back #define PF push_front #define MP make_pair typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const long long RRR = 1000000000000000000LL; struct Matrix { long long a[2][2]; public: Matrix() {} Matrix(LL b) { if(b==1) { a[0][0] = 1; a[0][1]=1; a[1][0]=1; a[1][1]=0; } else if(b==0) { a[0][0] = 1; a[1][1]=1; a[0][1] = 0; a[1][0]=0; } } Matrix multMod(Matrix, long long); }; unsigned long long multLongMod(unsigned long long a, unsigned long long b, long long M) { int cur = 0; long long res = 0; while(b > 0) { LL c = b%10; //assert(b>0); b/=10; unsigned long long cc = c*a; FOR(i,1,cur) { cc *= 10; cc %= RRR; } res = (res + cc)%RRR; cur++; } return res%M; } Matrix Matrix::multMod(Matrix b, long long M) { Matrix temp(0); //FOR(i,0,1) FOR(j,0,1) temp.a[i][j] = ((a[i][0]*b.a[0][j])%M + (a[i][1]*b.a[1][j])%M)%M; FOR(i,0,1) FOR(j,0,1) temp.a[i][j] = (multLongMod(a[i][0],b.a[0][j],M) + multLongMod(a[i][1],b.a[1][j],M))%M; return temp; } Matrix powMod(LL n, LL M) { Matrix temp(0); if(n==0) return temp; temp = powMod(n/2,M); temp = temp.multMod(temp,M); if(n%2 == 0) return temp; else { Matrix temp2(1); temp = temp.multMod(temp2,M); return temp; } } unsigned long long fibMod(LL k, LL M) { Matrix temp(0); temp = powMod(k+1,M); return temp.a[1][1]; } unsigned long long a[50]; unsigned long long poww10(LL k) { long long r = 1; FOR(i,1,k) r*=10; return r; } string s; int main() { //FOR(i,1,1000) { // cout << fibMod(i,100) << "\n"; //} a[0]=1; a[1]=60; /*for(LL r=2; r <= 18; r++) { for(LL k=1; k<=100; k++) { if(fibMod(a[r-1]*k,poww10(r)) == 0 && fibMod(a[r-1]*k+1,poww10(r)) == 1) { a[r] = a[r-1]*k; cout << r << " " << a[r] << "\n"; fflush(stdout); break; } } } */ a[2]=300; a[3]=1500; unsigned long long cur = 0; FOR(i,4,18) a[i]=10LL*a[i-1]; cin >> s; //cout << SZ(s) << "\n"; FOR(j,0,SZ(s)-1) { cur = 10*cur + (s[j]-'0'); } //cout << cur << "\n"; if(cur < 10000) { int ok = 0; FOR(i,60,15061) { if(fibMod(i,poww10(SZ(s))) == cur) { cout << i << "\n"; ok=1; break; } } if(ok==0) cout << "NIE\n" << "\n"; } else { //printf("tu"); //cout << cur << "\n"; set<unsigned long long> kand; FOR(i,60,30061) { if(fibMod(i,poww10(4LL)) == cur%10000) { kand.insert(i); //cout << i << "\n"; } } int ind = 5; while(!kand.empty()) { if(poww10(ind) > cur) break; set< unsigned long long> newkand; FOREACH(it,kand) { for(unsigned long long i=0; i <= 10; i++) if(fibMod((*it) + (i*a[ind-1]),poww10(ind)) == (cur%poww10(ind))) { newkand.insert(((*it) + i*a[ind-1])%poww10(ind)); //cout << ind << " " <<((*it) + i*a[ind-1])%poww10(ind) << "\n"; } } ind++; kand = newkand; } if(kand.empty()) cout << "NIE\n"; else cout << *(kand.begin()) << "\n"; } /* unsigned long long kk = 0; FORD(i,SZ(s)-1,0) { unsigned long long cur = 0; FOR(j,i,SZ(s)-1) { cur = 10*cur + (s[j]-'0'); } int lenn = SZ(s)-i; while(fibMod(kk,poww10(lenn))!=cur) { kk += a[lenn-1]; } cout << cur << " " << kk << "\n"; fflush(stdout); } cout << kk << "\n"; */ //cout << fibMod(60,100) << "\n"; //cout << fibMod(61,100) << "\n"; return 0; } |