#include <iostream> #include <iomanip> #include <cstdint> #include <map> #include <vector> #include <string> #include <sstream> #include <unordered_map> #include <map> #include <iterator> #include <algorithm> const uint64_t TEN_TO_18 = 1000UL*1000UL*1000*1000*1000*1000; const uint64_t ERROR = 0xFFFFFFFFFFFFFFFFUL; std::multimap<uint64_t, uint64_t> lookup3; typedef unsigned char digit; typedef std::vector<digit> bigint; std::ostream& operator<<(std::ostream& out, const bigint &i) { std::copy(i.rbegin(), i.rend(), std::ostream_iterator<short>(out)); return out; } bigint& reduceCarry(bigint& i) { digit carry = 0; for (size_t d=0; d<i.size(); ++d) { i[d] += carry; carry = i[d]/10; i[d] %= 10; } while (carry != 0) { i.push_back(carry%10); carry /= 10; } return i; } bigint& operator+=(bigint& i, const bigint& rhs) { size_t d = 0; for (; d<i.size() && d<rhs.size(); ++d) { i[d] += rhs[d]; } while (d<rhs.size()) { i.push_back(rhs[d++]); } return reduceCarry(i); } bigint operator+(const bigint& lhs, const bigint& rhs) { bigint ret = lhs; return ret += rhs; } bigint& rotLeft(bigint &lhs, unsigned short n) { lhs.insert(lhs.begin(), n, 0); return lhs; } bigint& operator*=(bigint &lhs, const bigint& rhs) { bigint pre = lhs; bigint::const_iterator rIt = rhs.begin(); std::transform(lhs.begin(), lhs.end(), lhs.begin(), std::bind2nd(std::multiplies<digit>(), *rIt)); reduceCarry(lhs); if (rIt == rhs.end()) return lhs; rotLeft(pre, 1); for (++rIt; rIt != rhs.end(); ++rIt) { bigint m = pre; size_t j = std::distance(rhs.begin(), rIt); std::transform( m.begin() + j, m.end(), m.begin() + j, std::bind2nd(std::multiplies<digit>(), *rIt)); lhs += m; rotLeft(pre, 1); } return lhs; } bigint operator*(const bigint &lhs, const bigint &rhs) { bigint ret = lhs; return ret *= rhs; } bigint from18Digits(uint64_t i) { bigint n; while(i != 0) { n.push_back(i%10); i/=10; } if(n.empty())n.push_back(0); return n; } uint64_t to18Digits(const bigint& b) { uint64_t ret = 0; uint64_t pow = 1; for (size_t i=0;i<b.size() && i<18; ++i) { ret += b[i] * pow; pow *= 10; } return ret; } uint64_t mulAndAdd(uint64_t a, uint64_t b, uint64_t c, uint64_t d) { bigint aa = from18Digits(a); bigint bb = from18Digits(b); aa *= bb; uint64_t sum = to18Digits(aa); bigint cc = from18Digits(c); bigint dd = from18Digits(d); cc *= dd; sum += to18Digits(cc); return sum % TEN_TO_18; } struct Mat22 { uint64_t a,b, c,d; Mat22& operator+=(const Mat22& m) { a += m.a; b += m.b; c += m.c; d += m.d; a %= TEN_TO_18; b %= TEN_TO_18; c %= TEN_TO_18; d %= TEN_TO_18; return *this; } Mat22& operator*=(const Mat22& m) { uint64_t na,nb,nc,nd; na = mulAndAdd(a, m.a, b, m.c); nb = mulAndAdd(a, m.b, b, m.d); nc = mulAndAdd(c, m.a, d, m.c); nd = mulAndAdd(c, m.b, d, m.d); a = na; b = nb; c = nc; d = nd; return *this; } }; struct QuickFibbData { std::vector<Mat22> pows; QuickFibbData() { uint64_t p = 1; Mat22 m = { 1, 1, 1, 0 }; for (int i=0; i<64; ++i) { pows.push_back(m); m *= m; p *= 2; } } }; uint64_t quickFibb18(uint64_t k) { static QuickFibbData d; if (k == 0) return 0; const uint64_t one = 1; Mat22 ret = {1, 1, 1, 0}; uint64_t nn = k-1; while (nn != 0) { uint64_t j = d.pows.size() - 1; for (; (one << j) > nn; --j){}; ret *= d.pows[j]; nn -= (one << j); } return ret.c; } uint64_t deeper(size_t digits, size_t checking, uint64_t tenMod, uint64_t targetNumber18d, uint64_t k) { uint64_t f = quickFibb18(k); //std::clog << "qf: " << f << " from " << k << " while checking " << checking << std::endl; if ((f % tenMod) == (targetNumber18d % tenMod)) { uint64_t step = 15*(tenMod/10); if (digits == checking) { // return value for next loop, to be sure about number of digits // 0 case for 18 digits return k + step; } for (int i=0; i<10; ++i) { uint64_t kk = deeper(digits, checking + 1, tenMod*10, targetNumber18d, k); if (kk != ERROR) { return kk; } k += step; } } return ERROR; } void verify(size_t digits, uint64_t number) { uint64_t nMod1000 = number % 1000; auto it = lookup3.lower_bound(nMod1000); if (it != lookup3.end() && it->first == nMod1000) { auto en = lookup3.upper_bound(nMod1000); while (it != en) { // we know first 3 digits matches, // we can do 10 loops of adding i*1500 to get chance of matching 4th digit // we know 4 digits loops on k=15000, right? uint64_t k = it->second; uint64_t step = 1500; for (int i=0; i<10; ++i) { uint64_t kk = deeper(digits, 4, 10000UL, number, k); if (kk != ERROR) { std::cout << kk << std::endl; return; } k += step; } ++it; } } std::cout << "NIE" << std::endl; } int main() { uint64_t a=0,b=1; uint64_t loops[] = {0, 60, 300, 1500, 15000 /*...*/}; for (int i=0; i<1500; ++i) { // std::clog << i << ": " << quickFibb18(i) << std::endl; // std::clog << i << ": " << a << std::endl; lookup3.insert(std::make_pair(a,i)); uint64_t c = (a+b) % 1000; a = b; b = c; } std::string digits; std::cin >> digits; uint64_t conv; std::stringstream convStr(digits); convStr >> std::dec >> conv; if (digits.size() <= 3) { auto it = lookup3.lower_bound(conv); if (it != lookup3.end() && it->first == conv) { std::cout << it->second + loops[3] << std::endl; } else { std::cout << "NIE" << std::endl; } return 0; } verify(digits.size(), conv); 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | #include <iostream> #include <iomanip> #include <cstdint> #include <map> #include <vector> #include <string> #include <sstream> #include <unordered_map> #include <map> #include <iterator> #include <algorithm> const uint64_t TEN_TO_18 = 1000UL*1000UL*1000*1000*1000*1000; const uint64_t ERROR = 0xFFFFFFFFFFFFFFFFUL; std::multimap<uint64_t, uint64_t> lookup3; typedef unsigned char digit; typedef std::vector<digit> bigint; std::ostream& operator<<(std::ostream& out, const bigint &i) { std::copy(i.rbegin(), i.rend(), std::ostream_iterator<short>(out)); return out; } bigint& reduceCarry(bigint& i) { digit carry = 0; for (size_t d=0; d<i.size(); ++d) { i[d] += carry; carry = i[d]/10; i[d] %= 10; } while (carry != 0) { i.push_back(carry%10); carry /= 10; } return i; } bigint& operator+=(bigint& i, const bigint& rhs) { size_t d = 0; for (; d<i.size() && d<rhs.size(); ++d) { i[d] += rhs[d]; } while (d<rhs.size()) { i.push_back(rhs[d++]); } return reduceCarry(i); } bigint operator+(const bigint& lhs, const bigint& rhs) { bigint ret = lhs; return ret += rhs; } bigint& rotLeft(bigint &lhs, unsigned short n) { lhs.insert(lhs.begin(), n, 0); return lhs; } bigint& operator*=(bigint &lhs, const bigint& rhs) { bigint pre = lhs; bigint::const_iterator rIt = rhs.begin(); std::transform(lhs.begin(), lhs.end(), lhs.begin(), std::bind2nd(std::multiplies<digit>(), *rIt)); reduceCarry(lhs); if (rIt == rhs.end()) return lhs; rotLeft(pre, 1); for (++rIt; rIt != rhs.end(); ++rIt) { bigint m = pre; size_t j = std::distance(rhs.begin(), rIt); std::transform( m.begin() + j, m.end(), m.begin() + j, std::bind2nd(std::multiplies<digit>(), *rIt)); lhs += m; rotLeft(pre, 1); } return lhs; } bigint operator*(const bigint &lhs, const bigint &rhs) { bigint ret = lhs; return ret *= rhs; } bigint from18Digits(uint64_t i) { bigint n; while(i != 0) { n.push_back(i%10); i/=10; } if(n.empty())n.push_back(0); return n; } uint64_t to18Digits(const bigint& b) { uint64_t ret = 0; uint64_t pow = 1; for (size_t i=0;i<b.size() && i<18; ++i) { ret += b[i] * pow; pow *= 10; } return ret; } uint64_t mulAndAdd(uint64_t a, uint64_t b, uint64_t c, uint64_t d) { bigint aa = from18Digits(a); bigint bb = from18Digits(b); aa *= bb; uint64_t sum = to18Digits(aa); bigint cc = from18Digits(c); bigint dd = from18Digits(d); cc *= dd; sum += to18Digits(cc); return sum % TEN_TO_18; } struct Mat22 { uint64_t a,b, c,d; Mat22& operator+=(const Mat22& m) { a += m.a; b += m.b; c += m.c; d += m.d; a %= TEN_TO_18; b %= TEN_TO_18; c %= TEN_TO_18; d %= TEN_TO_18; return *this; } Mat22& operator*=(const Mat22& m) { uint64_t na,nb,nc,nd; na = mulAndAdd(a, m.a, b, m.c); nb = mulAndAdd(a, m.b, b, m.d); nc = mulAndAdd(c, m.a, d, m.c); nd = mulAndAdd(c, m.b, d, m.d); a = na; b = nb; c = nc; d = nd; return *this; } }; struct QuickFibbData { std::vector<Mat22> pows; QuickFibbData() { uint64_t p = 1; Mat22 m = { 1, 1, 1, 0 }; for (int i=0; i<64; ++i) { pows.push_back(m); m *= m; p *= 2; } } }; uint64_t quickFibb18(uint64_t k) { static QuickFibbData d; if (k == 0) return 0; const uint64_t one = 1; Mat22 ret = {1, 1, 1, 0}; uint64_t nn = k-1; while (nn != 0) { uint64_t j = d.pows.size() - 1; for (; (one << j) > nn; --j){}; ret *= d.pows[j]; nn -= (one << j); } return ret.c; } uint64_t deeper(size_t digits, size_t checking, uint64_t tenMod, uint64_t targetNumber18d, uint64_t k) { uint64_t f = quickFibb18(k); //std::clog << "qf: " << f << " from " << k << " while checking " << checking << std::endl; if ((f % tenMod) == (targetNumber18d % tenMod)) { uint64_t step = 15*(tenMod/10); if (digits == checking) { // return value for next loop, to be sure about number of digits // 0 case for 18 digits return k + step; } for (int i=0; i<10; ++i) { uint64_t kk = deeper(digits, checking + 1, tenMod*10, targetNumber18d, k); if (kk != ERROR) { return kk; } k += step; } } return ERROR; } void verify(size_t digits, uint64_t number) { uint64_t nMod1000 = number % 1000; auto it = lookup3.lower_bound(nMod1000); if (it != lookup3.end() && it->first == nMod1000) { auto en = lookup3.upper_bound(nMod1000); while (it != en) { // we know first 3 digits matches, // we can do 10 loops of adding i*1500 to get chance of matching 4th digit // we know 4 digits loops on k=15000, right? uint64_t k = it->second; uint64_t step = 1500; for (int i=0; i<10; ++i) { uint64_t kk = deeper(digits, 4, 10000UL, number, k); if (kk != ERROR) { std::cout << kk << std::endl; return; } k += step; } ++it; } } std::cout << "NIE" << std::endl; } int main() { uint64_t a=0,b=1; uint64_t loops[] = {0, 60, 300, 1500, 15000 /*...*/}; for (int i=0; i<1500; ++i) { // std::clog << i << ": " << quickFibb18(i) << std::endl; // std::clog << i << ": " << a << std::endl; lookup3.insert(std::make_pair(a,i)); uint64_t c = (a+b) % 1000; a = b; b = c; } std::string digits; std::cin >> digits; uint64_t conv; std::stringstream convStr(digits); convStr >> std::dec >> conv; if (digits.size() <= 3) { auto it = lookup3.lower_bound(conv); if (it != lookup3.end() && it->first == conv) { std::cout << it->second + loops[3] << std::endl; } else { std::cout << "NIE" << std::endl; } return 0; } verify(digits.size(), conv); return 0; } |