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
感觉是最难的题了。用二分法进行查找。但是我一直无法AC,总差第一个测试点不通过,当我修改求二分法的中间点的公式的时候,结果就会争取,当会出现大量运行超时的情况。我就很纳闷到底哪里出现错误了。求大神指正。
下面的程序是我自己写的,我一开始是3个测试点不过,后来对上界进行重新处理。然后两个测试点过了。但第一个测试点一直找不出问题所在。把long long mid=(Last+Front)/2;变成long long mid=(Last+Front)/2+1;第一个测试点就能通过了。哎,25分只能拿24分了,那一分太难了。求大神指导。我就不放其他大神的正确答案了。
过了一天我终于弄出来个AC的了,把long long mid=(Last+Front)/2;变换为long long mid=(Last+Front+1)/2;就能通过。估计是因为小数的计算取整的问题可能使一些数无法被测试。
下面是AC的程序:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
long long BecomeTen(int Number[],int NumberSize,int Radix){
long long NumberTen=0;
for(int i=0;i<NumberSize;i++){
NumberTen=NumberTen*Radix+Number[i];
}
return NumberTen;
}
long long FindRadix(int Number1[],long long Number2,long long Front,long long Last,int i){
long long mid=(Last+Front+1)/2;
long long number1;
number1=BecomeTen(Number1,i,mid);
if(number1==Number2){
return mid;
}
else if(Front>Last){
return -1;
}
else if(number1>Number2||number1<0){
mid=mid-1;
return FindRadix(Number1,Number2,Front,mid,i);
}
else{
mid=mid+1;
return FindRadix(Number1,Number2,mid,Last,i);
}
}
int main(){
string S1,S2;
long long tag,radix;int MaxNumber1,MaxNumber2;
int Number1[100],Number2[100];
MaxNumber1=MaxNumber2=0;
cin>>S1>>S2>>tag>>radix;
int i=S1.size();
int j=S2.size();
for(int k=0;k<i;k++){
if(S1[k]>='0'&&S1[k]<='9'){
Number1[k]=S1[k]-'0';
if(Number1[k]>=MaxNumber1){
MaxNumber1=Number1[k]+1;
}
}
else {
Number1[k]=S1[k]-'a'+10;
if(Number1[k]>=MaxNumber1){
MaxNumber1=Number1[k]+1;
}
}
}
for(int k=0;k<j;k++){
if(S2[k]>='0'&&S2[k]<='9'){
Number2[k]=S2[k]-'0';
if(Number2[k]>=MaxNumber2){
MaxNumber2=Number2[k]+1;
}
}
else{
Number2[k]=S2[k]-'a'+10;
if(Number2[k]>=MaxNumber2){
MaxNumber2=Number2[k]+1;
}
}
}
long long number1,number2;
number1=BecomeTen(Number1,i,radix);
number2=BecomeTen(Number2,j,radix);
long long Radix;
long long Max;
if(number1>number2)
Max=number1;
else Max=number2;
if(tag==1){
Radix=FindRadix(Number2,number1,MaxNumber2,Max,j);
}
else{
Radix=FindRadix(Number1,number2,MaxNumber1,Max,i);
}
if(Radix>0){
printf("%lld\n",Radix);
}
else{
printf("Impossible\n");
}
}