1010 Radix (25 分)

1010 Radix (25 分)

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

 =============================================

参考了柳婼的代码,但我没有模块化代码。

题意:

题目给两个数A、B,字母或数字组成,数A给出进制,轻松转化为10进制。

数B的进制未知,需要你找出某个进制 x 使。得A == B,如果找不到则输出 Impossible.

一句话:判断俩数都化为10进制是否相等。一数已知10进制,另一个数让你找出进制x (可能找不到)满足相等。

自然想到穷举进制 x。

=============================================

以下太长建议跳过直接看代码,如果你不理解代码,再回来看。

思路:

只干一件事,那就是:确定进制x的范围。(因为还有找不到的情况,不能无限穷举下去,过了某个范围就退出并宣告失败)

x最小值 = 数B中最大的那那一位数字  + 1;

为什么?x 是数B的进制,进制至少比数B中最大的那位数字大1。

比如1223,最少得是4进制,如果是2进制,那么数字2和3不可能出现。同理不能是3进制,因为有3的出现。

其他同理,进制至少是最大值 + 1;

 x最大值 = max(sumN1 和 x最小值);// sumN1是已知进制的数在10进制下的值。

举例说明:

CASE 1: x最大值取sumN1;x范围是[x最小值,sumN1]

假设sumN1 = 300

进制 x 使得数B转为10进制的sumN2 == sumN1才算成功找到。

如果进制x = 300,则数B当且仅当为 “10” 时两者相等。

如果进制x = 301,

则数B找不到任何一个数使两者相等。//想一想如果只有一位,sumN2为0-9,如果有2位,则至少>=301,两者永不等。

CASE 2:x最大值取x最小值;

进制 x 只有唯一确定值,如果这个进制下sumN2不等于sumN1, 则俩数永不等。

实际中发现,穷举x时采用二分法更快,故用二分法穷举,说到底还是高级点的穷举~

=============================================

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
	long long tag, sumN1 = 0, sumN2 = 0, radix;
	string n1, n2;
	cin >> n1 >> n2 >> tag >> radix;
	if(tag == 2) swap(n1, n2);//永远把n1当作已知进制的数,寻找n2的数制 
	long long len = n1.size();
	for(int i = 0; i < len; i++){//转为10进制 
		if(isdigit(n1[i]))
				sumN1 += (n1[i] - '0') * pow(radix, len - i - 1);
		else sumN1 += (10 + n1[i] - 'a') * pow(radix, len - i - 1);
	}
	char it = *max_element(n2.begin(), n2.end());//寻找数B中最大数。
 	long long low = (isdigit(it) ? it - '0': it - 'a' + 10) + 1;//进制x最小值:数B最大+1;
 	long long high = max(sumN1, low);//进制x最大值
 	len = n2.length();
 	while (low <= high) {
 		sumN2 = 0;
 		long long mid = (low + high) / 2;
		for(int j = 0; j < len; j++){//以mid为radix转为10进制 
			if(isdigit(n2[j]))
				sumN2 += (n2[j] - '0') * pow(mid, len - j - 1);
			else
				sumN2 += (10 + n2[j] - 'a') * pow(mid, len - j - 1);
		}
 		if (sumN2 < 0 || sumN2 > sumN1){//sumN2有可能太大溢出变为负值,故加上sumN2<0
		 	high = mid - 1;
		} else if (sumN2 == sumN1){   //相等吗?不相等则改变high和low,二分法
			printf("%d", mid);
			return 0;	
		} else
			low = mid + 1;
	}
 	printf("Impossible");
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值