#include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; ll mult(ll a, ll b, ll m) { if(b==0) return 0; if(b%2==0) return mult(2*a%m, b/2, m); return (mult(2*a%m, b/2, m)+a)%m; } struct Matrix //Yay! Macierze!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! { ll t[2][2]; //Tak, naprawde }; Matrix mult(Matrix a, Matrix b, ll m) { Matrix res; for(int i=0; i<2; ++i) for(int j=0; j<2; ++j) res.t[i][j]=0; for(int i=0; i<2; ++i) for(int k=0; k<2; ++k) for(int j=0; j<2; ++j) res.t[i][j]=(res.t[i][j]+mult(a.t[i][k], b.t[k][j], m))%m; return res; } const Matrix identity() { Matrix res; res.t[1][0]=res.t[0][1]=0; res.t[0][0]=res.t[1][1]=1; return res; } Matrix pow(Matrix a, ll n, ll m) { if(n==0) return identity(); if(n%2==0) return pow(mult(a, a, m), n/2, m); return mult(pow(mult(a, a, m), n/2, m), a, m); } ll fibonacci(ll n, ll m) { Matrix mat; mat.t[0][0]=0; mat.t[0][1]=mat.t[1][0]=mat.t[1][1]=1; return pow(mat, n, m).t[0][1]; } char s[20]; int num_2[1LL<<18], num_2a[1LL<<18]; void init(int n) { for(int i=1; i<(1LL<<(n)); ++i) num_2a[i]=-1; int a=0, b=1; for(int i=1; i<(1LL<<(n-1))*3; ++i) { swap(a, b); b=(a+b)%(1LL<<n); if(num_2[a]==0) num_2[a]=i; else num_2a[a]=i; } } ll period(int n) { ll tmp=1; if(n==1) return 60; if(n==2) return 300; while(n--) tmp*=10; return 3*tmp/2; } ll period2(int n) { if(n==1) return 3; if(n==2) return 6; return (1LL<<(n-1))*3; } int main() { scanf("%s", s); int n=strlen(s); ll value=0; for(int i=0; i<n; ++i) { value*=10; value+=s[i]-'0'; } if(n<=2) { ll a=0, b=1, i=2; bool hi=0; ll m=1; while(n--) m*=10; for(;i<1000; ++i) { swap(a, b); if(a+b>=m) hi=1; b=(a+b)%m; if(hi&&b==value%m) break; } if(i==1000) printf("NIE\n"); else printf("%lld", i); //printf("TAK\n"); return 0; } init(n); if(value==0) { printf("%lld", period(n)); //printf("TAK\n"); return 0; } if(num_2[value%(1LL<<n)]==0) { printf("NIE\n"); return 0; } ll m=5, m10=10; ll res[4], res1[4]; int j=0; for(int i=0; i<20; ++i) if(fibonacci(i, 5)==value%5) res[j++]=i; for(int i1=1; i1<n; ++i1) { m*=5; m10*=10; j=0; for(int i=0; i<4; ++i) for(ll k=res[i]; k<4*m; k+=4*m/5) if(fibonacci(k, m)==value%m) res1[j++]=k; for(int i=0; i<4; ++i) res[i]=res1[i]; } for(j=0; j<4; ++j) { for(; res[j]%period2(n)!=num_2[value%(1LL<<n)]&&res[j]%period2(n)!=num_2a[value%(1LL<<n)]&&res[j]<period(n); res[j]+=4*m); if(fibonacci(res[j], m10)==value) printf("%lld", res[j]+period(n)); //if(res[j]%period2(n)!=num_2[value%(1LL<<n)]) printf("TAK\n"); if(fibonacci(res[j], m10)==value) return 0; } return 0; } /* 864 1386 2364 2886 3864 4386 5364 5886 6864 7386 8364 8886 9864 */
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 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; ll mult(ll a, ll b, ll m) { if(b==0) return 0; if(b%2==0) return mult(2*a%m, b/2, m); return (mult(2*a%m, b/2, m)+a)%m; } struct Matrix //Yay! Macierze!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! { ll t[2][2]; //Tak, naprawde }; Matrix mult(Matrix a, Matrix b, ll m) { Matrix res; for(int i=0; i<2; ++i) for(int j=0; j<2; ++j) res.t[i][j]=0; for(int i=0; i<2; ++i) for(int k=0; k<2; ++k) for(int j=0; j<2; ++j) res.t[i][j]=(res.t[i][j]+mult(a.t[i][k], b.t[k][j], m))%m; return res; } const Matrix identity() { Matrix res; res.t[1][0]=res.t[0][1]=0; res.t[0][0]=res.t[1][1]=1; return res; } Matrix pow(Matrix a, ll n, ll m) { if(n==0) return identity(); if(n%2==0) return pow(mult(a, a, m), n/2, m); return mult(pow(mult(a, a, m), n/2, m), a, m); } ll fibonacci(ll n, ll m) { Matrix mat; mat.t[0][0]=0; mat.t[0][1]=mat.t[1][0]=mat.t[1][1]=1; return pow(mat, n, m).t[0][1]; } char s[20]; int num_2[1LL<<18], num_2a[1LL<<18]; void init(int n) { for(int i=1; i<(1LL<<(n)); ++i) num_2a[i]=-1; int a=0, b=1; for(int i=1; i<(1LL<<(n-1))*3; ++i) { swap(a, b); b=(a+b)%(1LL<<n); if(num_2[a]==0) num_2[a]=i; else num_2a[a]=i; } } ll period(int n) { ll tmp=1; if(n==1) return 60; if(n==2) return 300; while(n--) tmp*=10; return 3*tmp/2; } ll period2(int n) { if(n==1) return 3; if(n==2) return 6; return (1LL<<(n-1))*3; } int main() { scanf("%s", s); int n=strlen(s); ll value=0; for(int i=0; i<n; ++i) { value*=10; value+=s[i]-'0'; } if(n<=2) { ll a=0, b=1, i=2; bool hi=0; ll m=1; while(n--) m*=10; for(;i<1000; ++i) { swap(a, b); if(a+b>=m) hi=1; b=(a+b)%m; if(hi&&b==value%m) break; } if(i==1000) printf("NIE\n"); else printf("%lld", i); //printf("TAK\n"); return 0; } init(n); if(value==0) { printf("%lld", period(n)); //printf("TAK\n"); return 0; } if(num_2[value%(1LL<<n)]==0) { printf("NIE\n"); return 0; } ll m=5, m10=10; ll res[4], res1[4]; int j=0; for(int i=0; i<20; ++i) if(fibonacci(i, 5)==value%5) res[j++]=i; for(int i1=1; i1<n; ++i1) { m*=5; m10*=10; j=0; for(int i=0; i<4; ++i) for(ll k=res[i]; k<4*m; k+=4*m/5) if(fibonacci(k, m)==value%m) res1[j++]=k; for(int i=0; i<4; ++i) res[i]=res1[i]; } for(j=0; j<4; ++j) { for(; res[j]%period2(n)!=num_2[value%(1LL<<n)]&&res[j]%period2(n)!=num_2a[value%(1LL<<n)]&&res[j]<period(n); res[j]+=4*m); if(fibonacci(res[j], m10)==value) printf("%lld", res[j]+period(n)); //if(res[j]%period2(n)!=num_2[value%(1LL<<n)]) printf("TAK\n"); if(fibonacci(res[j], m10)==value) return 0; } return 0; } /* 864 1386 2364 2886 3864 4386 5364 5886 6864 7386 8364 8886 9864 */ |