- 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
#include<cstdio>
#include<cstring>
using namespace std;
int tag;
char s1[11],s2[11];
int r;
long long deal(char s[],int r){
int i=0;
long long sum=0;
while(s[i]!='\0'){
int n=0;
if(s[i]>='0'&&s[i]<='9'){
n=s[i]-'0';
}
else if(s[i]>='a'&&s[i]<='z'){
n=s[i]-'a'+10;
}
sum=sum*r+n;
i++;
}
return sum;
}
int find(char s[]){
int i=0;
int h=1;
while(s[i]!='\0'){
int n=0;
if(s[i]>='0'&&s[i]<='9'){
n=s[i]-'0';
}
else if(s[i]>='a'&&s[i]<='z'){
n=s[i]-'a'+10;
}
i++;
if(n>h) h=n;
}
return h+1;
}
int main(){
scanf("%s %s",s1,s2);
scanf("%d %d",&tag,&r);
int minr1=find(s1);
int minr2=find(s2);
if(tag==1){
if(r<minr1){
printf("Impossible");
return 0;
}
long long a=deal(s1,r);
long long b=0;
int i=minr2;
while(b<a){
b=deal(s2,i);
i++;//超时发生在这里,逐一探测太慢,需要改进
}
if(b==a)printf("%d",i-1);
else printf("Impossible");
}
if(tag==2){
if(r<minr2){
printf("Impossible");
return 0;
}
long long a=deal(s2,r);
long long b=0;
int i=minr1;
while(b<a){
b=deal(s1,i);
i++;
}
if(b==a)printf("%d",i-1);
else printf("Impossible");
}
return 0;
}
本文介绍了一个基数转换的问题,即给定两个正整数N1和N2,在已知其中一个数的基数的情况下,找出另一个数的基数使得这两个数相等。文章通过具体的样例展示了如何解决这个问题,并提供了一段C++代码实现。
487

被折叠的 条评论
为什么被折叠?



