400 ms

65536 kB

16000 B

Standard

CHEN, Yue

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.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
Here N1 and N2 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, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

Output Specification:

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, print "Impossible". If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10

Sample Output 1:
2

Sample Input 2:
1 ab 1 2

Sample Output 2:
Impossible

#### 注意点：

（1）使用二分查找，暴力查找会超时

（2）用long long储存，因为给定的数最多有10位，在大一点的进制下很容易超出int储存范围

#### c++代码：

#include<bits/stdc++.h>
using namespace std;
unordered_map<char,int>charToint;//记录0-9,a-z字符对应的进制
long long result=0;
for(int i=0;s[i]!='\0';++i)
return result;
}
for(int i=0;n2[i]!='\0';++i)//找到要查找的进制数的下限
left=max(left,(long long)charToint[n2[i]]+1);
while(left<right){//二分查找第一个使得n2指向的字符串表示的数大于等于tag的进制
long long mid=left+(right-left)/2,k=trans(n2,mid);
if(k<0||k>=tag)//k<0时表示数据已经超出long long存储范围
right=mid;
else if(k<tag)
left=mid+1;
}
if(trans(n2,left)!=tag)//如果查找到的进制下两数不相等，返回-1
left=-1;
return left;
}
int main(){
char s1[11],s2[11];
for(int i=0;i<36;++i)//将0-9,a-z字符对应的进制压入charToint的map中，方便进行转换
charToint.insert({i<10?i+'0':i-10+'a',i});
printf("Impossible");
else//否则直接输出
return 0;
}

• 擅长领域：
• 算法