1010 . Radix (25)
时间限制
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:
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 <cmath>
#include <string>
using namespace std;
long long change(string a, long long radix) {
long long sum = 0;
long long temp = 0;
for (long long i = a.length()-1; i >= 0; i--) {
if (a[i] >= 'a' && a[i] <= 'z') {
sum += (a[i] - 'a' + 10) * pow(radix, temp++);
} else if (a[i] >= '0' && a[i] <= '9') {
sum += (a[i] - '0') * pow(radix, temp++);
}
}
return sum;
}
long long getLowRadix(string a) {
long long least = 0;
long long num = 0;
for (long long i = 0; i < a.length(); i++) {
if (a[i] >= 'a' && a[i] <= 'z')
num = a[i] - 'a' + 10;
else if (a[i] >= '0' && a[i] <= '9')
num = a[i] - '0';
if (num+1 > least)
least = num + 1;
}
return least;
}
long long compare(string a, long long radix, long long target) {
long long temp = 0, sum = 0;
for (long long i = a.length() - 1; i >= 0; i--) {
if (a[i] >= 'a' && a[i] <= 'z')
sum += (a[i] - 'a' + 10) * pow(radix, temp++);
else if (a[i] >= '0' && a[i] <= '9')
sum += (a[i] - '0') * pow(radix, temp++);
if (sum > target)
return 1;
}
if (sum > target)
return 1;
else if (sum < target)
return -1;
else
return 0;
}
long long binarySearch(string a, long long low, long long high, long long target) {
long long mid = low;
long long temp;
while (low <= high) {
temp = compare(a, mid, target);
if (temp > 0) {
high = mid - 1;
} else if (temp < 0) {
low = mid + 1;
} else {
return mid;
}
mid = (low + high) / 2;
}
return -1;
}
int main() {
string A, B;
long long tag, radix, least = 0, most = 0;
long long target = 0, result = 0;
cin >> A >> B >> tag >> radix;
if (tag == 1) {
target = change(A, radix);
least = getLowRadix(B);
most = (target + 1 > least + 1) ? target + 1 : least + 1;
result = binarySearch(B, least, most, target);
if (result == -1) {
cout << "Impossible";
} else {
cout << result;
}
} else if (tag == 2) {
target = change(B, radix);
least = getLowRadix(A);
most = (target + 1 > least + 1) ? target + 1 : least + 1;
result = binarySearch(A, least, most, target);
if (result == -1) {
cout << "Impossible";
} else {
cout << result;
}
}
return 0;
}