1010. Radix (25)

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

题目:

1.输入N1,N2,tag,radix;N1,N2是两串数,其中位数大于10,每位数都为正数,取值范围:‘0-9’表示数字0-9,‘a-z’表示数字10-35;tag为1则radix表示N1的进制,tag为2则radix表示N2的进制数

2.寻找是否存在另一个的某种进制,使得两个数相等。

3.若该进制存在,则输出该进制,否则输出Impossible

思路:

1.根据tag转换,使得N1为已知进制数;

2.首先将已知进制的数N1换算成10进制;

3.未知进制的数N2,其进制查找范围为,下界为N2中最大数+1(注意:最小进制为2),上届为N1的十进制数(最小进制为2);

注意:

1.最小进制为2

2.数据类型需为long long int

3.需要采用二分法加剪枝(计算到n2>n1就可以停止这次10进制换算了),否则可能超时

4.在10进制换算中,可能有溢出的情况

#include<iostream>
#include<string>
using namespace std;
long long n1=-1;
long long getValue(string str,long long r)//10进制换算 
{
	long long value=0;
	for(int i=0;i<str.length();i++)
	{
		if(str[i]>='0'&&str[i]<='9')value+=(str[i]-'0');
		if(str[i]>='a'&&str[i]<='z')value+=(str[i]-'a'+10);
		if(i!=str.length()-1)value*=r;
		if(value<0)return -1;//数据溢出
		if(n1!=-1&&value>n1)return -1;//n2的剪枝 
	}
	return value;
}
long long getLowRadix(string str)//得到二分法下界 
{
	long long lowR=2;
	long long num;
	for(int i=0;i<str.length();i++)
	{
		if(str[i]>='0'&&str[i]<='9')num=str[i]-'0';
		if(str[i]>='a'&&str[i]<='z')num=str[i]-'a'+10;
		if(num>=lowR)lowR=num+1;
	}
	return lowR;
}
int main()
{
	string N1,N2,tmp;
	long long tag,radix,n2;
	long long ansR=-1;
	long long lowR,highR,midR;
	cin>>N1>>N2>>tag>>radix;
	if(tag==2)
	{
		tmp=N1;
		N1=N2;
		N2=tmp;
	}
	n1=getValue(N1,radix);
	lowR=getLowRadix(N2);
	if(n1>=2)highR=n1+1;
	else highR=2;
	while(lowR<=highR)
	{
		midR=(lowR+highR)/2;
		n2=getValue(N2,midR);
		if(n2==n1)
		{
			ansR=midR;
			break;
		}
		else if(n2>n1||n2<0)
		{
			highR=midR-1;
		}
		else 
		{
			lowR=midR+1;
		}
	}
	if(ansR==-1)cout<<"Impossible\n";
	else cout<<ansR<<endl;
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值