Radix(PATA-1010)
题面
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is
yes
, if 6 is a decimal number and 110 is a binary number.Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.
输入
Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
1N1 N2 tag radix
Here
N1
andN2
each has no more than 10 digits. A digit is less than its radix and is chosen from the set { 0-9,a
-z
} where 0-9 represent the decimal numbers 0-9, anda
-z
represent the decimal numbers 10-35. The last numberradix
is the radix ofN1
iftag
is 1, or ofN2
iftag
is 2.
输出
For each test case, print in one line the radix of the other number so that the equation
N1
=N2
is true. If the equation is impossible, printImpossible
. If the solution is not unique, output the smallest possible radix.
样例输入1
16 110 1 10
样例输出1
12
样例输入2
11 ab 1 2
样例输出2
1Impossible
提示
无
思路
二分。
代码
1const int inf = 0x3f3f3f3f;
2const int mxn = 1e6 + 5;
3
4char a[mxn], b[mxn];
5
6LL getnum(char *s, LL r){
7 LL ans = 0;
8 for(; *s; s++)
9 ans = ans * r + (*s - (*s <= '9' ? '0' : 'a'-10));
10 return ans;
11}
12
13LL getans(char *s, char *t, LL r){
14 char mx = *max_element(t, t+strlen(t));
15 LL l = mx - (mx <= '9' ? '0' : 'a'-10) + 1;
16 LL f=0, at = getnum(s, r); r = max(l, at);
17 while(l <= r){
18 LL m = (l + r) / 2;
19 LL bt = getnum(t, m);
20 if(bt < 0 || bt > at){
21 r = m - 1;
22 }else if(at == bt){
23 f = m;
24 break;
25 }else{
26 l = m + 1;
27 }
28 }
29 return f;
30}
31
32int main()
33{
34 LL t, r;
35 scanf("%s %s %lld %lld", a, b, &t, &r);
36 LL f = (t==1) ? getans(a, b, r) : getans(b, a, r);
37 if(f){
38 printf("%lld\n", f);
39 }else{
40 printf("Impossible\n");
41 }
42
43 return 0;
44}