-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1010-1.cpp
60 lines (54 loc) · 1.05 KB
/
A1010-1.cpp
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
/*
* author: zhouyuhao
* created: 2023-03-27 10:58:14
* modified: 2023-03-31 19:16:35
* item: Programming Ability Test
* site: Yuting
*/
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll trans(string s, ll b) {
ll sum = 0, w = 1;
for (ll i = s.size() - 1; i >= 0; i--) {
if (isdigit(s[i])) {
sum += (s[i] - '0') * w;
} else {
sum += (s[i] - 'a' + 10) * w;
}
w *= b;
}
return sum;
}
int main(int argc, char const *argv[]) {
string n1, n2;
ll tag, radix;
cin >> n1 >> n2 >> tag >> radix;
if (tag == 2) {
swap(n1, n2);
}
ll num = trans(n1, radix);
char ch = *max_element(n2.begin(), n2.end());
ll low = (isdigit(ch) ? ch - '0' : ch - 'a' + 10) + 1;
ll high = max(low, num);
ll ans = -1;
while (low <= high) {
ll mid = (low + high) >> 1;
ll temp = trans(n2, mid);
if (temp > num || temp < 0) {
high = mid - 1;
} else if (temp < num) {
low = mid + 1;
} else {
ans = mid;
break;
}
}
if (ans != -1) {
cout << ans << "\n";
} else {
cout << "Impossible\n";
}
return 0;
}