1010 Radix:猥琐的测试数据

谨以此题纪念边界测试数据浪费了我多少时间:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 
 6 void read(std::vector<int>& _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         else
15             _data.push_back(c - 'a' + 10);
16 }
17 
18 auto convert(const std::vector<int>& _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<int>& _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<uint64_t>(*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         else
46             return mid;
47     }
48     return -1;
49 }
50 
51 int main(int argc, char const *argv[])
52 {
53     std::vector<int> data;
54     int64_t value;
55     {
56         std::vector<int> 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     else
72         std::cout << res;
73     
74     return 0;
75 }
原文地址:https://www.cnblogs.com/jerry-fuyi/p/11183431.html