#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
using namespace std;
long toDecimal(string str,int radix){
reverse(str.begin(),str.end());
long res=0;
for(unsigned int i=0;i<str.length();i++){
if(str[i]>='0'&&str[i]<='9'){
res+=(str[i]-'0')*pow(radix,i);
}else{
res+=(str[i]-'a'+10)*pow(radix,i);
}
}
return res;
}
int main(){
string N1,N2;
int tag,radix;
cin>>N1>>N2>>tag>>radix;
long res1,res2;
string a;
if(tag==1){
res1=toDecimal(N1,radix);
a=N2;
}else{
res1=toDecimal(N2,radix);
a=N1;
}
int flag=0,max=0;
for(unsigned int i=0;i<a.length();i++){
int temp;
if(a[i]>='0'&&a[i]<='9'){
temp=a[i]-'0';
}else{
temp=a[i]-'a'+10;
}
if(temp>max){
max=temp;
}
}
long left=max+1,right=res1+1;
while(left<=right){
long mid=(left+right)/2;
res2=toDecimal(a,mid);
if(res2==res1){
cout<<mid;
flag=1;
break;
}else if(res2>res1||res2<0){
right=mid-1;
}else{
left=mid+1;
}
}
if(flag==0){
cout<<"Impossible";
}
return 0;
}
刷题向 | 1010 Radix
最新推荐文章于 2022-02-24 17:51:52 发布