1010 Radix(Binary Search)

本文探讨了如何通过基数转换和二分查找算法解决两个数在不同进制下相等的问题,介绍了基数转换函数和确定可能进制范围的方法。

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;
} 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值