-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1010.cpp
69 lines (61 loc) · 1.18 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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
typedef long long lint;
int trans(char c) {
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'a' && c <= 'z')
return c - 'a' + 10;
return 0;
}
lint s2int(string s, int radix) {
lint result = 0;
for(int i = 0; i < s.size(); ++i) {
result = result * radix + trans(s[i]);
if(result < 0) {
return -1;
}
}
return result;
}
int main(int argc, char const *argv[])
{
string s1;
string s2;
int tag, radix;
cin >> s1 >> s2 >> tag >> radix;
if(2 == tag)
swap(s1, s2);
lint a = 0;
for(int i = 0; i < s1.size(); ++i) {
a = a * radix + trans(s1[i]);
}
int minRadix = -1;
for(int i = 0; i < s2.size(); ++i) {
minRadix = max( minRadix, trans(s2[i]) );
}
++minRadix;
lint cur = 0, pre = -1;
lint low = max(minRadix, 2), high = a + 1, mid;
lint result = -1;
while(low <= high) {
mid = (low + high) / 2;
cur = s2int(s2, mid);
if( cur >= a || -1 == cur ){
if( cur == a && (mid < result || -1 == result) )
result = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
if(result != -1){
cout << result << endl;
}
else
cout << "Impossible" << endl;
return 0;
}