-
Notifications
You must be signed in to change notification settings - Fork 0
/
1010.cpp
94 lines (82 loc) · 1.4 KB
/
1010.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL; //防溢出,用long long
//LL inf = (1LL << 63) - 1;
LL to_real(string a, LL radix){
LL re = 0;
for(string::iterator it = a.begin(); it != a.end(); it++){
if(*it >= '0' && *it <= '9'){
re = re*radix + (*it - '0');
}
else{
re = re*radix + (*it - 'a' + 10);
}
}
return re;
}
LL max(LL a, LL b){
if(a > b){
return a;
}
else{
return b;
}
}
int main(){
string a, b;
LL tag, radix;
cin >> a;
cin >> b;
scanf("%lld %lld", &tag, &radix);
LL real = 0;
string an, num;
if(tag == 1){
real = to_real(a, radix);
an = b;
}
else{
real = to_real(b, radix);
an = a;
}
LL left = -1;
for(string::iterator it = an.begin(); it != an.end(); it++){
if(*it >= '0' && *it <= '9'){
if(*it - '0' > left){
left = *it - '0';
}
}
else{
if(*it - 'a' + 10> left){
left = *it - 'a' + 10;
}
}
}
left++;
LL right = max(left, real) + 1; //进制数最大不会超过真值(超过两位数)和left中的最大值
LL ans = -1;
//二分
while(left <= right){
LL mid = (left + right) / 2;
LL mid_val = to_real(an, mid);
if(mid_val < 0 || mid_val > real){ //注意溢出
right = mid - 1;
}
else if(mid_val == real){
ans = mid;
break;
}
else{
left = mid + 1;
}
}
if(ans != -1){
printf("%lld\n", ans);
}
else{
printf("Impossible\n");
}
return 0;
}