题目描述:
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:
N1 N2 tag radix
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
题目解释:
简单来说就是,字符串n1,n2代表两个数,而且不知道进制,n1,n2均不超过10位
tag为1时,radix是n1的进制,tag为二时,radix是n2的进制(这里就可以知道两者之一的大小)
题目要求在知道一个数的大小情况下(我们知道n1或者n2的大小),求一个合适的进制Radix,使得另外那个数等于已知的那个数,能就输出Radix,不能就输出Impossible
那个radix不唯一的情况实际就是双方都为0的那种情况,应该输出2
做题思路:
我们要比较两个数,就应该把两个数转化为十进制来比较
所以我们首先要把已知数转化为十进制,然后再找进制使另外等于已知
那么我们找进制的范围就使为(a,b)
a=未知进制的那个数的最大的某一位数+1,这个应该不难理解
b=已知进制数+1,这个有点难理解
然后我们采用二分查找来找进制
注意点:
1.可能会有溢出,所以我们要用long long,
即使用了long long 也会溢出,溢出会小于0,我们可以根据是否小于0来判断
2.特殊情况,就是n1和n2都为0
代码:
#include <bits/stdc++.h>
using namespace std;
long long turndecimal(const string n, const int radix)
{
int len = n.length();
long long sum = 0;
for (int i = len - 1; i >= 0; i--)
{
char ch = n[i];
int tmp = ((ch >= '0') && (ch <= '9')) ? (ch - '0') : (ch - 'a' + 10);
sum += pow(radix, (len - 1 - i)) * tmp;
}
return sum;
}
long long findthemax(string str)
{
int len = str.length();
long long max = 0;
for (int i = 0; i < len; i++)
{
char ch = str[i];
long long tmp = ((ch >= '0') && (ch <= '9')) ? (ch - '0') : (ch - 'a' + 10);
if (tmp > max)
{
max = tmp;
}
}
if (max < 1)
{
max = 1;
}
return max;
}
long long findradix(long long low, long long high, const string str, const long long decimal)
{
if (low > high)
{
return -1;
}
else
{
long long mid = (low + high) / 2;
long long decimal_ = turndecimal(str, mid);
if (decimal == decimal_)
{
return mid;
}
else if (decimal < decimal_ || decimal_ < 0)
{
return findradix(low, mid - 1, str, decimal);
}
else
{
return findradix(mid + 1, high, str, decimal);
}
}
}
int main()
{
string n1, n2;
int tag, radix;
cin >> n1 >> n2 >> tag >> radix;
if (n1 == "0" && n2 == "0")
{
cout << "2" << endl;
return 0;
}
long long decimal = 0;
if (tag == 1)
{
long long maxnumber = findthemax(n2);
decimal = turndecimal(n1, radix);
long long res = findradix(maxnumber + 1, decimal + 1, n2, decimal);
if (res != -1)
{
cout << res << endl;
}
else
{
cout << "Impossible" << endl;
}
}
else
{
long long maxnumber = findthemax(n1);
decimal = turndecimal(n2, radix);
long long res = findradix(maxnumber + 1, decimal + 1, n1, decimal);
if (res != -1)
{
cout << res << endl;
}
else
{
cout << "Impossible" << endl;
}
}
return 0;
}