1010 Radix(Binary Search)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LightInDarkness/article/details/84931136

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 N​1​​ and N​2​​, 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

Analyze:

    This problem is not as easy as I thought and frankly I sought help on the Internet. Fisrt of all, we are supposed to convert the number given the radix into a decimal number thus we have a function named Convert(). What we need to do next is to find the right radix and we use binary search to save time, which is not hard. Unfortunately, here's a question that confused me most. How can we ascertain the range of radices? There's is no doubt that the lowest radix is higher than every digit of the num. 

    So...what is upper limit? Given two numbers a(uncertain) and b(decimal number):

    If a has one digit: the upper limit is a + 1. In other words, there's no discrepancy between lower limit and upper limit.

    If a has more than two digits, we can set upper limit = b:

    ①When a == 10, there's a possibility that a * b + 0 == b;

    ②When a > 10, a must be greater than b.

    If upper limit is greater than b, there's no way that a can be equal to b. Hence upper limit b is a advisable choice.

    In conclusion, we can set upper limit = max(a + 1, b).

#include<iostream>
#include<cmath>

using namespace std;

long long Convert(string num, long long radix){
	long long result = 0;
	int index = 0;
	for(int i = num.length() - 1; i >= 0; i--){
		if(num[i] >= '0' && num[i] <= '9')
			result += (num[i] - '0') * pow(radix, index++);
		else
			result += (num[i] - 'a' + 10) * pow(radix, index++);
	}
	return result;
}

long long FindRadix(string num, long long n){
	char maxChar = num[0];
	long long low, high;
	for(int i = 1; i < num.length(); i++)
		if(num[i] > maxChar) maxChar = num[i];
	if(maxChar >= '0' && maxChar <= '9') low = (maxChar - '0') + 1;
	else low = (maxChar - 'a' + 10) + 1;
	high = max(low, n);
	while(low <= high){
		long long mid = (low + high) / 2;
		long long temp = Convert(num, mid);
		//如果temp < 0, 说明转换后的数字过大, 否则应当能在long long范围内表示 
		if(temp < 0 || temp > n) high = mid - 1;
		else if(temp == n) return mid;
		else low = mid + 1;
	}
	return -1;
}

int main(){
	string n1, n2;
	int tag, radix;
	long long tmp, solve;
	cin >> n1 >> n2 >> tag >> radix;
	if(tag == 1){
		tmp = Convert(n1, radix);
		solve = FindRadix(n2, tmp);
	}else{
		tmp = Convert(n2, radix);
		solve = FindRadix(n1, tmp);
	}
	if(solve != -1) cout << solve;
	else cout << "Impossible";
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/LightInDarkness/article/details/84931136
今日推荐