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
思路:题目进制可能非常大,使用二分法就OK了,左边界是各位中最大数字加1,右边界是数值+1.全部用long long,中途还要避免溢出
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char str1[12],str2[12];
long long n,m=1;
int get_min_rad(int len,char *str){
int num,min_rad=1;
for(int i=0;i<len;i++){
num=str[i]<'a'?str[i]-'0':str[i]-'a'+10;
min_rad=max(num+1,min_rad);
}
return min_rad;
}
long long get_N1(int len,char *str,long long radix){
//printf("radix=%lld\n",radix);
long long num=0;
int t;
num=str[0]<'a'?str[0]-'0':str[0]-'a'+10;
for(int i=1;i<len;i++){
t=str[i]<'a'?str[i]-'0':str[i]-'a'+10;
if(num>(m-t)/radix)//已经大于,直接返回,可以避免溢出
return n+1;
num=num*radix+t;
}
return num;
}
long long search(int len,char *str,long long left,long long right){
long long mid,num=0;
//printf("%n==lld\n",n);
while(left<=right){
mid=left+(right-left)/2;
num=get_N1(len,str,mid);
//printf("%num==lld\n",num);
if(num==n){
return mid;
}
else if(num>n){
right=mid-1;
}
else{
left=mid+1;
}
}
return -1;
}
long long solve(int len,char *str){
int min_rad=get_min_rad(len,str);
long long ans=search(len,str,min_rad,n+1);
if(ans>0&&len==1)
ans=min_rad;
return ans;
}
int main(){
//freopen("in.txt","r",stdin);
int tag,radix;
scanf("%s%s%d%d",str1,str2,&tag,&radix);
int len1,len2,min_rad=1;
long long ans;
len1=strlen(str1);
len2=strlen(str2);
m=(m<<63)-1;
if(tag==1){
min_rad=get_min_rad(len1,str1);
if(radix<min_rad){
ans=-1;
}
else{
n=get_N1(len1,str1,radix);
m=n;
ans=solve(len2,str2);
}
}
else{
min_rad=get_min_rad(len2,str2);
if(radix<min_rad){
ans=-1;
}
else{
n=get_N1(len2,str2,radix);
m=n;
ans=solve(len1,str1);
}
}
if(ans>0){
printf("%lld\n",ans);//ans可能是long long
}
else{
printf("Impossible\n");
}
return 0;
}