-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA1010.cpp
72 lines (69 loc) · 1.38 KB
/
A1010.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
61
62
63
64
65
66
67
68
69
70
71
72
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const long long INF = (1LL << 63) - 1;
struct Number{
char digit[12];
int radix;
int minRadix(){
int max = -1;
int len = strlen(digit);
for(int i = 0; i < len; ++i){
int tmp;
if(digit[i] >= '0' && digit[i] <= '9'){
tmp = digit[i] - '0';
}else{
tmp = digit[i] - 'a' + 10;
}
if(tmp > max){
max = tmp;
}
}
return max + 1;
}
long long decimal(){
long long ans = 0;
long long weight = 1;
int len = strlen(digit);
for(int i = len - 1; i >= 0; --i){
int tmp;
if(digit[i] >= '0' && digit[i] <= '9'){
tmp = digit[i] - '0';
}else{
tmp = digit[i] - 'a' + 10;
}
ans += tmp * weight;
weight *= radix;
if(ans < 0) return -1;
}
return ans;
}
} num1, num2;
int binarySearch(){
long long x = num1.decimal();
long long left = num2.minRadix(), right = max(left, x) + 1, mid;
while(left <= right){
mid = (left + right) / 2;
num2.radix = mid;
if(num2.decimal() == x) return mid;
if(num2.decimal() < 0 || num2.decimal() > x){
right = mid - 1;
}else{
left = mid + 1;
}
}
return -1;
}
int main(){
scanf("%s%s", num1.digit, num2.digit);
int flag, radix;
scanf("%d%d", &flag, &radix);
if(flag == 2){
swap(num1.digit, num2.digit);
}
num1.radix = radix;
int ans = binarySearch();
if(ans == -1) printf("Impossible");
else printf("%d", ans);
}