-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpat-1010.cpp
More file actions
66 lines (62 loc) · 1.41 KB
/
Copy pathpat-1010.cpp
File metadata and controls
66 lines (62 loc) · 1.41 KB
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
#include <cstdio>
#include <cctype>
#include <climits>
int getd(const char& c) {
if (isdigit(c)) {
return c - '0';
} else {
return c - 'a' + 10;
}
}
long long convert(char* n, long long radix, long long max) {
long long r = 0;
while (*n != 0) {
int num = getd(*n);
if ((LLONG_MAX - num) / radix < r) { // LLONG_MAX exceed
return max + 1;
}
r = radix * r + num;
n++;
}
return r;
}
int test(char* n1, long long radix, char* n2) {
long long nn1 = convert(n1, radix, LLONG_MAX-1);
long long left = 1, right = LLONG_MAX, now, m;
for(int i=0; n2[i] != 0; i++) {
int n=getd(n2[i]);
left = left > n ? left : n;
}
left++;
while (left < right) {
m = (right - left) / 2 + left;
now = convert(n2, m, nn1);
if (now == nn1) {
right = m;
} else if (now > nn1) {
right = m - 1;
} else {
left = m + 1;
}
}
if (convert(n2, left, LLONG_MAX-1) != nn1)
left = -1;
return left;
}
int main() {
char n1[10], n2[10];
long long radix, r;
int tag;
scanf("%s %s %d %lld", n1, n2, &tag, &radix);
if (tag == 1) {
r = test(n1, radix, n2);
} else {
r = test(n2, radix, n1);
}
if (r == -1) {
puts("Impossible");
} else {
printf("%lld", r);
}
return 0;
}