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
这道题考虑越界的情况,另外需要用二分查找去减少时间开销。思路容易有,但细节需要注意。
#include<iostream>
#include<cstring>
using namespace std;
int char2num(char s)
{
if (s >= '0'&&s <= '9') return (s - '0');
else if (s >= 'a'&&s <= 'z') return (s - 'a' + 10);
}
long long int str2dec(char *s, int len, long long int radix)
{
long long radix_c = 1;
int i = len - 1;
long long int temp = 0;
while (1){
if (i == -1) return temp;
temp += char2num(s[i--])*radix_c;
radix_c = radix_c*radix;
}
return temp;
}
//二分查找
long long int find_radix(long start, long end, long num, char *s, int len)
{
for (long long int i = start; i <= end; i++)
{
long long int k = str2dec(s, len, i);
if (k == num) return i;
else if (k > num) return 0;
else
{
long long int t = (start + end) / 2;
long long int k = str2dec(s, len, t);
if (k < num) start = t+1;
else if (k == num) return t;
else
{
end = t-1; start += 1;
}
//cout << start << "," << end << endl;
}
}
return 0;
}
long long int is_equal(char *s1, char *s, char flag, long long int radix)
{
int len = strlen(s);
int len1 = strlen(s1);
int temp1 = 0;
char j = s[0];
int i = 0;
long long int start, end;
//cout << s1 << "," << len1 << "," << radix << endl;
long long int num = str2dec(s1, len1, radix);
//cout << num << "num" << endl;
//确定搜索域
for (i = 0; i < len; i++)
{
if (s[i]>j) j = s[i];
}
if (flag == 1)
{
start = radix + 1;
if (char2num(j) > start)
start = char2num(j) + 1;
end = num + 1;
}
else
{
start = char2num(j) + 1;
end = radix;
}
//cout << start <<','<< end << endl;
return find_radix(start, end, num, s, len);
}
int main()
{
char ch;
char str1[11], str2[11];
int tag;
long long int radix;
long long int num1, num2;
long long int radix2 = 0;
int len1 = 0, len2 = 0;
//cout << str2dec("110", 3, 2) << endl;
char flag = 0;
while (1)
{
ch = getchar();
if (ch == ' ') break;
str1[len1++] = ch;
}
str1[len1] = '\0';
while (1)
{
ch = getchar();
if (ch == ' ') break;
str2[len2++] = ch;
}
str2[len2] = '\0';
cin >> tag >> radix;
//通过判定str1和str2的大小,来确定搜索域
if (len1 > len2) flag = 1;
else if (len1 == len2)
{
if (strcmp(str1, str2) > 0) flag = 1;
else if (strcmp(str1, str2) == 0)
{
cout << radix << endl;
return 0;
}
else
flag = 0;
}
else
flag = 0;
if (tag == 1)
{
flag = flag;
//cout << num1 << endl;
radix2 = is_equal(str1, str2, flag, radix);
if (radix2)
cout << radix2 << endl;
else
cout << "Impossible" << endl;
}
else
{
flag ^= 1;
//cout << num2 << endl;
radix2 = is_equal(str2, str1, flag, radix);
if (radix2)
cout << radix2 << endl;
else
cout << "Impossible" << endl;
}
return 0;
}