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

例如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;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值