谨以此题纪念边界测试数据浪费了我多少时间:
1 #include2 #include 3 #include 4 #include 5 6 void read(std::vector & _data) 7 { 8 std::string s; 9 std::cin >> s;10 _data.reserve(s.size());11 for (auto c : s)12 if (c < 'a')13 _data.push_back(c - '0');14 else15 _data.push_back(c - 'a' + 10);16 }17 18 auto convert(const std::vector & _data, uint64_t _radix)19 {20 uint64_t res = 0;21 for (auto i : _data)22 res = res * _radix + i;23 return res;24 }25 26 uint64_t solve(const std::vector & _data, uint64_t _value)27 {28 if (_data.size() == 1)29 return _data[0] == _value ? _value + 1 : -1;30 uint64_t radix;31 for (radix = 2; convert(_data, radix) <= _value; radix *= 2)32 ;33 if (radix == 2)34 return -1;35 auto begin = std::max (*std::max_element(_data.begin(), _data.end()) + 1, radix / 2);36 auto end = radix;37 while (begin < end)38 {39 auto mid = (begin + end) / 2;40 auto res = convert(_data, mid);41 if (res > _value)42 end = mid;43 else if (res < _value)44 begin = mid + 1;45 else46 return mid;47 }48 return -1;49 }50 51 int main(int argc, char const *argv[])52 {53 std::vector data;54 int64_t value;55 {56 std::vector source;57 read(source);58 read(data);59 int which;60 std::cin >> which;61 if (which == 2)62 source.swap(data);63 int radix;64 std::cin >> radix;65 value = convert(source, radix);66 }67 68 auto res = solve(data, value);69 if (res == -1)70 std::cout << "Impossible";71 else72 std::cout << res;73 74 return 0;75 }