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
例如tag=1时,n1的进制范围时2~36;但是string n1长度很长,而n2长度很短,若都转化为十进制n1=n2,那么n2的进制就不仅仅时2~36了。
思路:相同一个数字,进制越大,那么表示的真值越大,因此可以用二分法。
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
#include <stack>
#include <list>
#include <cstdio>
#include <map>
#include <set>
#include <iomanip>
#include <cmath>
using namespace std;
string n1, n2;
long long int to10(string s, long long int d) {//string转化为10进制
long long int sum = 0, temp;
for (int i = 0; i < s.length(); i++) {
if (s[i] >= 'a'&&s[i] <= 'z') {
s[i] = s[i] - 'a' + 10;
}
if (s[i] >= '0'&&s[i] <= '9') {
s[i] = s[i] - '0';
}
temp = s[i];
sum = sum * d + temp;
}
return sum;
}
long long int find(long long int d1, long long int d2, long long int x, string s) {
long long int left = d1, right = d2;
while (left <= right) {
long long int mid = (left + right) / 2;
long long int temp = to10(s, mid);
if (temp > x||temp<0) {//temp<0表示在该进制的情况下,temp超出long long int表示范围,所以往左区间寻找
right = mid - 1;
}
else if (temp == x) {
return mid;
}
else {
left = mid + 1;
}
}
return -1;
}
int main()
{
long long int res, res2;
long long int tag, radix;
cin >> n1 >> n2 >> tag >> radix;
if (n1 == n2) {
cout << radix << endl;
return 0;
}
if (tag == 1) {
long long int min = 0, maxi;
for (int i = 0; i < n2.length(); i++)
{
if (isdigit(n2[i])) {
if (n2[i] - '0' > min)
min = n2[i] - '0' + 1;
}
else if (isalpha(n2[i])) {
if (n2[i] - 'a' + 10 > min)
min = n2[i] - 'a' + 10 + 1;
}
}
res = to10(n1, radix);
maxi = max(res, min);
long long int mid = find(min, maxi, res, n2);
if (mid == -1) {
cout << "Impossible" << endl;
return 0;
}
else {
cout << mid;
}
}
if (tag == 2) {
long long int min = 0, maxi;
for (int i = 0; i < n1.length(); i++)
{
if (isdigit(n1[i])) {
if (n1[i] - '0' > min)
min = n1[i] - '0' + 1;
}
else if (isalpha(n1[i])) {
if (n1[i] - 'a' + 10 > min)
min = n1[i] - 'a' + 10 + 1;
}
}
res2 = to10(n2, radix);
maxi = max(res2, min);
long long int mid = find(min, maxi, res2, n1);
if (mid == -1) {
cout << "Impossible" << endl;
return 0;
}
else {
cout << mid;
}
}
}