#include <bits/stdc++.h> #include <string> using namespace std; template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& data) { out << data[0]; for( auto it = data.begin()+1; it != data.end(); it++ ) out << ' ' << *it; return out; } template<typename T> inline istream& operator>> (istream& in, vector<T>& data) { for( auto &v : data ) in >> v; return in; } struct uint128_t { uint64_t hi, lo; uint128_t(uint64_t v) : hi(0), lo(v) {} uint128_t(uint64_t hi, uint64_t lo) : hi(hi), lo(lo) {} explicit operator uint64_t() const { return lo; } friend uint128_t operator<< (const uint128_t a, int n) { uint128_t ret {a.hi<<1, a.lo<<1}; if( a.lo bitand (1ULL<<63) ) ret.hi++; return ret; } friend uint128_t operator>> (const uint128_t a, int n) { uint128_t ret {a.hi>>1, a.lo>>1}; if( a.hi bitand 1 ) ret.lo |= (1ULL<<63); return ret; } friend uint128_t operator+(const uint128_t a, const uint128_t b) { uint128_t ret {a.hi+b.hi, a.lo+b.lo}; if( a.lo+b.lo < a.lo or a.lo+b.lo < b.lo ) ret.hi++; return ret; } friend uint128_t operator-(const uint128_t a, const uint128_t b) { uint128_t ret {a.hi-b.hi, a.lo-b.lo}; if( a.lo < b.lo ) ret.hi--; return ret; } friend uint128_t operator*(uint128_t a, uint64_t b) { uint128_t ret {0, 0}; while( b ) { if( b%2 == 1 ) { ret = ret + a; } a = a + a; b /= 2; } return ret; } friend bool operator<(const uint128_t a, const uint128_t b) { if( a.hi != b.hi ) return a.hi < b.hi; return a.lo < b.lo; } friend bool operator>=(const uint128_t a, const uint128_t b) { return not (a < b); } friend uint128_t operator%(uint128_t a, const uint128_t b) { uint128_t c = b; while( a >= c ) c = c << 1; while( c >= b ) { if( a >= c ) { a = a - c; } c = c >> 1; } return a; } friend ostream& operator<< (ostream& out, const uint128_t& a) { return out << '(' << a.hi << ", " << a.lo << ')'; } }; struct matrix_t { uint64_t a, b, c; static const matrix_t one, base; friend matrix_t mul(const matrix_t& x, const matrix_t& y, const uint64_t mod) { return matrix_t { (uint64_t)((uint128_t(x.a)*y.a + uint128_t(x.b)*y.b)%uint128_t(mod)), (uint64_t)((uint128_t(x.a)*y.b + uint128_t(x.b)*y.c)%uint128_t(mod)), (uint64_t)((uint128_t(x.b)*y.b + uint128_t(x.c)*y.c)%uint128_t(mod)) }; } }; const matrix_t matrix_t::one {1, 0, 1}, matrix_t::base {0, 1, 1}; const auto fib_init = pair<uint64_t, uint64_t>( 0, 1 ); pair<uint64_t, uint64_t> fib(uint64_t n, uint64_t mod) { matrix_t ret = matrix_t::one, val = matrix_t::base; while( n ) { if( n%2 == 1 ) ret = mul( ret, val, mod ); val = mul( val, val, mod ); n /= 2; } return make_pair(ret.b, ret.c); } /*inline uint64_t stoll(string s) { istringstream in(s); uint64_t ret; in >> ret; return ret; }*/ void test() { string Cstr; cin >> Cstr; uint64_t C = stoll(Cstr); uint64_t mod = 1; uint64_t cycle_length = 1; vector<uint64_t> begins; begins.push_back(0); for( int i = 0; i < Cstr.size(); i++ ) { mod *= 10; uint64_t Cmod = C%mod; uint64_t new_cycle_length = cycle_length; vector<uint64_t> new_begins; for( auto& begin : begins ) if( fib(begin, mod).first == Cmod ) new_begins.push_back(begin); while( fib(new_cycle_length, mod) != fib_init ) { for( auto begin : begins ) { begin += new_cycle_length; if( fib(begin, mod).first == Cmod ) new_begins.push_back(begin); } new_cycle_length += cycle_length; } cycle_length = new_cycle_length; begins.swap(new_begins); if( begins.size() == 0 ) { cout << "NIE\n"; return; } } cout << begins.front()+cycle_length << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; //cin >> T; while( T --> 0 ) test(); 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 | #include <bits/stdc++.h> #include <string> using namespace std; template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& data) { out << data[0]; for( auto it = data.begin()+1; it != data.end(); it++ ) out << ' ' << *it; return out; } template<typename T> inline istream& operator>> (istream& in, vector<T>& data) { for( auto &v : data ) in >> v; return in; } struct uint128_t { uint64_t hi, lo; uint128_t(uint64_t v) : hi(0), lo(v) {} uint128_t(uint64_t hi, uint64_t lo) : hi(hi), lo(lo) {} explicit operator uint64_t() const { return lo; } friend uint128_t operator<< (const uint128_t a, int n) { uint128_t ret {a.hi<<1, a.lo<<1}; if( a.lo bitand (1ULL<<63) ) ret.hi++; return ret; } friend uint128_t operator>> (const uint128_t a, int n) { uint128_t ret {a.hi>>1, a.lo>>1}; if( a.hi bitand 1 ) ret.lo |= (1ULL<<63); return ret; } friend uint128_t operator+(const uint128_t a, const uint128_t b) { uint128_t ret {a.hi+b.hi, a.lo+b.lo}; if( a.lo+b.lo < a.lo or a.lo+b.lo < b.lo ) ret.hi++; return ret; } friend uint128_t operator-(const uint128_t a, const uint128_t b) { uint128_t ret {a.hi-b.hi, a.lo-b.lo}; if( a.lo < b.lo ) ret.hi--; return ret; } friend uint128_t operator*(uint128_t a, uint64_t b) { uint128_t ret {0, 0}; while( b ) { if( b%2 == 1 ) { ret = ret + a; } a = a + a; b /= 2; } return ret; } friend bool operator<(const uint128_t a, const uint128_t b) { if( a.hi != b.hi ) return a.hi < b.hi; return a.lo < b.lo; } friend bool operator>=(const uint128_t a, const uint128_t b) { return not (a < b); } friend uint128_t operator%(uint128_t a, const uint128_t b) { uint128_t c = b; while( a >= c ) c = c << 1; while( c >= b ) { if( a >= c ) { a = a - c; } c = c >> 1; } return a; } friend ostream& operator<< (ostream& out, const uint128_t& a) { return out << '(' << a.hi << ", " << a.lo << ')'; } }; struct matrix_t { uint64_t a, b, c; static const matrix_t one, base; friend matrix_t mul(const matrix_t& x, const matrix_t& y, const uint64_t mod) { return matrix_t { (uint64_t)((uint128_t(x.a)*y.a + uint128_t(x.b)*y.b)%uint128_t(mod)), (uint64_t)((uint128_t(x.a)*y.b + uint128_t(x.b)*y.c)%uint128_t(mod)), (uint64_t)((uint128_t(x.b)*y.b + uint128_t(x.c)*y.c)%uint128_t(mod)) }; } }; const matrix_t matrix_t::one {1, 0, 1}, matrix_t::base {0, 1, 1}; const auto fib_init = pair<uint64_t, uint64_t>( 0, 1 ); pair<uint64_t, uint64_t> fib(uint64_t n, uint64_t mod) { matrix_t ret = matrix_t::one, val = matrix_t::base; while( n ) { if( n%2 == 1 ) ret = mul( ret, val, mod ); val = mul( val, val, mod ); n /= 2; } return make_pair(ret.b, ret.c); } /*inline uint64_t stoll(string s) { istringstream in(s); uint64_t ret; in >> ret; return ret; }*/ void test() { string Cstr; cin >> Cstr; uint64_t C = stoll(Cstr); uint64_t mod = 1; uint64_t cycle_length = 1; vector<uint64_t> begins; begins.push_back(0); for( int i = 0; i < Cstr.size(); i++ ) { mod *= 10; uint64_t Cmod = C%mod; uint64_t new_cycle_length = cycle_length; vector<uint64_t> new_begins; for( auto& begin : begins ) if( fib(begin, mod).first == Cmod ) new_begins.push_back(begin); while( fib(new_cycle_length, mod) != fib_init ) { for( auto begin : begins ) { begin += new_cycle_length; if( fib(begin, mod).first == Cmod ) new_begins.push_back(begin); } new_cycle_length += cycle_length; } cycle_length = new_cycle_length; begins.swap(new_begins); if( begins.size() == 0 ) { cout << "NIE\n"; return; } } cout << begins.front()+cycle_length << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; //cin >> T; while( T --> 0 ) test(); return 0; } |