#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> #include <sstream> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// const LL MOD = (LL)3e18 ; // algorytm mnozenia rosyjskich chlopow LL mult(LL a, LL b) { LL s=0 ; while(a>0) { if(a&1) s = (s+b)%MOD ; b = (b<<1)%MOD ; a >>= 1 ; } return s ; } const int SIZE = 2 ; struct matrix { LL t[SIZE][SIZE] ; matrix(int a=0) { CLEAR(t) ; int i ; REP(i,SIZE) t[i][i] = a ; } } ; matrix operator*(const matrix &A, const matrix &B) { matrix C ; int i, j, k ; REP(i, SIZE) REP(k, SIZE) REP(j, SIZE) { C.t[i][j] += mult(A.t[i][k], B.t[k][j]) ; C.t[i][j] %= MOD ; } return C ; } ///////////////////////////////////////////// void read_input(const string &s, int &n, LL &c) { n = s.size() ; istringstream stream(s) ; stream >> c ; } LL getFib(LL n) { matrix A ; A.t[0][0] = A.t[0][1] = A.t[1][0] = 1 ; return power(A,n).t[0][1] ; } // preprocessing: znalezione takie x, ze x*period2 + y*period5 = NWD(period2,period5) const LL x[19] = {0, 7LL,17LL,42LL,-104LL,-1302LL,-651LL,-8138LL,-4069LL,193278LL, -1856486LL,8837382LL,53246816LL,26623408LL,2454717954LL,-10979672273LL,-51266203324LL,126954788963LL,1207886574169LL} ; LL solve(string s) { int n, i, j ; LL c ; read_input(s, n,c) ; const LL period = 3*power((LL)2, max(2,n-1)) * power((LL)5, n) ; const LL mod = power((LL)10,n) ; const int d = (n>=3 ? 4 : n) ; const int period2 = 3*(1<<(n-1)) ; const int mod2 = 1<<n ; const int c2 = c%mod2 ; VI a(d, -1) ; int fib1=-1, fib2=1 ; REP(i, period2) { int tmp = (fib1+fib2)%mod2 ; fib1=fib2 ; fib2=tmp ; if(tmp==c2) { if(a[i%d]==-1) a[i%d] = i ; } } const LL period5 = 4*power((LL)5,n) ; const LL mod5 = power((LL)5,n) ; const LL c5 = c%mod5 ; vector<LL> k ; fib1=-1 ; fib2=1 ; REP(i, 20) { int tmp=(fib1+fib2)%5 ; fib1=fib2 ; fib2=tmp ; if(tmp == c5%5) k.PB(i) ; } //assert(k.size()==4) ; for(i=1 ; i<n ; i++) { vector<LL> next ; LL mod = power((LL)5,i) ; LL period = 4*mod ; FOREACH(it, k) { REP(j,5) { LL k2 = (*it) + j*period ; if(getFib(k2)%(5*mod) == c5%(5*mod)) next.PB(k2) ; } } k = next ; //assert(k.size()==4) ; } REP(i, 4) { if(a[i]==-1) continue ; FOREACH(b, k) { if((*b)%d != i) continue ; LL A = a[i], B = *b ; bool minus = (x[n]<0)^(B-A<0) ; LL tmp = mult(abs(x[n]),abs(B-A)) %period ; tmp = mult(tmp, period2/d) %period ; if(minus) tmp = (period-tmp)%period ; tmp = (A+tmp)%period ; LL ret = tmp+period ; //LL check = getFib(ret)%mod ; //aassert(check == c) ; return ret ; } } return -1 ; } int main() { ios_base::sync_with_stdio(0) ; string s ; while(cin >> s) { LL ret = solve(s) ; if(ret==-1) cout << "NIE" << endl ; else { //cout << "TAK" << endl ; cout << ret << endl ; } } }
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 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> #include <sstream> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// const LL MOD = (LL)3e18 ; // algorytm mnozenia rosyjskich chlopow LL mult(LL a, LL b) { LL s=0 ; while(a>0) { if(a&1) s = (s+b)%MOD ; b = (b<<1)%MOD ; a >>= 1 ; } return s ; } const int SIZE = 2 ; struct matrix { LL t[SIZE][SIZE] ; matrix(int a=0) { CLEAR(t) ; int i ; REP(i,SIZE) t[i][i] = a ; } } ; matrix operator*(const matrix &A, const matrix &B) { matrix C ; int i, j, k ; REP(i, SIZE) REP(k, SIZE) REP(j, SIZE) { C.t[i][j] += mult(A.t[i][k], B.t[k][j]) ; C.t[i][j] %= MOD ; } return C ; } ///////////////////////////////////////////// void read_input(const string &s, int &n, LL &c) { n = s.size() ; istringstream stream(s) ; stream >> c ; } LL getFib(LL n) { matrix A ; A.t[0][0] = A.t[0][1] = A.t[1][0] = 1 ; return power(A,n).t[0][1] ; } // preprocessing: znalezione takie x, ze x*period2 + y*period5 = NWD(period2,period5) const LL x[19] = {0, 7LL,17LL,42LL,-104LL,-1302LL,-651LL,-8138LL,-4069LL,193278LL, -1856486LL,8837382LL,53246816LL,26623408LL,2454717954LL,-10979672273LL,-51266203324LL,126954788963LL,1207886574169LL} ; LL solve(string s) { int n, i, j ; LL c ; read_input(s, n,c) ; const LL period = 3*power((LL)2, max(2,n-1)) * power((LL)5, n) ; const LL mod = power((LL)10,n) ; const int d = (n>=3 ? 4 : n) ; const int period2 = 3*(1<<(n-1)) ; const int mod2 = 1<<n ; const int c2 = c%mod2 ; VI a(d, -1) ; int fib1=-1, fib2=1 ; REP(i, period2) { int tmp = (fib1+fib2)%mod2 ; fib1=fib2 ; fib2=tmp ; if(tmp==c2) { if(a[i%d]==-1) a[i%d] = i ; } } const LL period5 = 4*power((LL)5,n) ; const LL mod5 = power((LL)5,n) ; const LL c5 = c%mod5 ; vector<LL> k ; fib1=-1 ; fib2=1 ; REP(i, 20) { int tmp=(fib1+fib2)%5 ; fib1=fib2 ; fib2=tmp ; if(tmp == c5%5) k.PB(i) ; } //assert(k.size()==4) ; for(i=1 ; i<n ; i++) { vector<LL> next ; LL mod = power((LL)5,i) ; LL period = 4*mod ; FOREACH(it, k) { REP(j,5) { LL k2 = (*it) + j*period ; if(getFib(k2)%(5*mod) == c5%(5*mod)) next.PB(k2) ; } } k = next ; //assert(k.size()==4) ; } REP(i, 4) { if(a[i]==-1) continue ; FOREACH(b, k) { if((*b)%d != i) continue ; LL A = a[i], B = *b ; bool minus = (x[n]<0)^(B-A<0) ; LL tmp = mult(abs(x[n]),abs(B-A)) %period ; tmp = mult(tmp, period2/d) %period ; if(minus) tmp = (period-tmp)%period ; tmp = (A+tmp)%period ; LL ret = tmp+period ; //LL check = getFib(ret)%mod ; //aassert(check == c) ; return ret ; } } return -1 ; } int main() { ios_base::sync_with_stdio(0) ; string s ; while(cin >> s) { LL ret = solve(s) ; if(ret==-1) cout << "NIE" << endl ; else { //cout << "TAK" << endl ; cout << ret << endl ; } } } |