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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
题意是:
N1 N2 tag radix ,N1,N2分别不超过十位,并且又数字和小写字母组成
当tag = 1的时候N1是radix的基数,当tag = 2的时候N2是radix的基数
找未知数的最小基数看未知数是否与已知数相等。
我把需要找基数的那个数称为 未知数。
坑点:基数的范围,基数的范围应该是无穷的,但是我们可以根据题意把基数局限在一个有效的范围,基数的下限是:未知数的最大字符 + 1,上限是:已知数。 因为我们要找基数来达到未知数 = 已知数的目的,那么未知数的基数是不可能大于已知数的。
找到上限和下限就可以二分了。
N1, N2都应该用string 转换后的数 要用 long long
参考了参考柳婼大神的代码 发现大神好喜欢用 三目运算符
学习到了很多新的函数:比如:*max_element(n.begin(), n.end());
convert函数是把字符串根据一个进制转换成相应的数字
find_radix函数是二分找基数