题目描述
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
代码一
此代码卡一测试案例…已找到原因:进制边界,假如一个数转换成十进制数a超长的话,有可能另一个数1,但是它要是a进制表示才可能与前一个数相等。而我在循环遍历的时候只考虑到了进制在100以内的情况。边界修改之后仍然会有一个问题就是:运行超时!这是因为循环次数太多了。可以利用二分查找进行优化。
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
long long to_ten(char *str,long long radix)
{
long long sum=0;
long long len=strlen(str);
for(long long i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9') sum+=pow(radix,len-1-i)*(str[i]-'0');
else if(str[i]>='a'&&str[i]<='z') sum+=pow(radix,len-1-i)*(str[i]-'a'+10);
}
return sum;
}
long long max_char(char *str)
{
char c=str[0];
long long len=strlen(str);
for(long long i=1;i<len;i++)
{
if(str[i]>c) c=str[i];
}
long long max;
if(c>='0'&&c<='9') max=c-'0';
else if(c>='a'&&c<='z') max=c-'a'+10;
return max;
}
int main()
{
int tag;
long long radix;
char n1[11],n2[11];
scanf("%s %s %d %lld",n1,n2,&tag,&radix);
//printf("%s %s %d %d",n1,n2,tag,radix);
//int sum=to_ten(n2,radix);
//printf("%d",sum);
long long a1,a2;
long long flag=-1;
if(tag==1)
{
a1=to_ten(n1,radix);
for(long long i=max_char(n2)+1;i<=100;i++)
{
a2=to_ten(n2,i);
if(a1==a2)
{
flag=i;
break;
}
}
}
else
{
a2=to_ten(n2,radix);
for(long long i=max_char(n1)+1;i<=100;i++)
{
a1=to_ten(n1,i);
if(a1==a2)
{
flag=i;
break;
}
}
}
if(flag==-1) printf("Impossible");
else printf("%lld",flag);
return 0;
}
代码二(AC代码)
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
long long to_ten(char *str,long long radix)
{
long long sum=0;
long long len=strlen(str);
for(long long i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9') sum+=pow(radix,len-1-i)*(str[i]-'0');
else if(str[i]>='a'&&str[i]<='z') sum+=pow(radix,len-1-i)*(str[i]-'a'+10);
}
return sum;
}
long long max_char(char *str)
{
char c=str[0];
long long len=strlen(str);
for(long long i=1;i<len;i++)
{
if(str[i]>c) c=str[i];
}
long long max;
if(c>='0'&&c<='9') max=c-'0';
else if(c>='a'&&c<='z') max=c-'a'+10;
return max+1;
}
long long find_radix(char *str,long long low,long long high,long long sum)
{
if(low>high) return -1;
else
{
long long mid=(low+high)/2;
long long count=to_ten(str,mid);
if(count==sum) return mid;
else if(count>sum||count<0)
{
return find_radix(str,low,mid-1,sum);
}
else return find_radix(str,mid+1,high,sum);
}
}
int main()
{
int tag;
long long flag,radix;
char n1[11],n2[11];
scanf("%s %s %d %lld",n1,n2,&tag,&radix);
if(tag==1)
{
long long right=max_char(n2)>to_ten(n1,radix)?max_char(n2):to_ten(n1,radix);
flag=find_radix(n2,max_char(n2),right+1,to_ten(n1,radix));
}
else
{
long long right=max_char(n1)>to_ten(n2,radix)?max_char(n1):to_ten(n2,radix);
flag=find_radix(n1,max_char(n1),right+1,to_ten(n2,radix));
}
if(flag==-1) printf("Impossible");
else printf("%lld",flag);
return 0;
}