先求出第一个值,然后二分第二个数的基数就可以
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
long long convert(string a, long long radix){
long long ret=0;
for(auto it=a.begin(); it!=a.end(); it++){
int i=isdigit(*it)?*it-'0':*it-'a'+10;
ret=ret*radix+i;
}
return ret;
}
char find_max(string a){
char temp='0';
for(int i=0; i<a.length(); i++)
temp=max(temp,a[i]);
return temp;
}
long long binarySearch(string num,long long target){
char c=find_max(num);
long long low=isdigit(c)?c-'0':c-'a';
low +=1;
long long high=max(low,target+1);
long long mid;
long long res=-1;
while(low<=high){
mid=low+((high-low)>>1);
long long t=convert(num,mid);
if(t==target){
res=mid;
high=mid-1;
}
else if(t<0||t>target)
high=mid-1;
else
low=mid+1;
}
return res;
}
int main(int argc, char const *argv[])
{
string a,b;
long long tag,radix;
cin>>a>>b>>tag>>radix;
if(tag==1){
long long target=convert(a,radix);
long long res=binarySearch(b,target);
if(res==-1LL)
cout<<"Impossible";
else
cout<<res;
}else{
long long target=convert(b,radix);
long long res=binarySearch(a,target);
if(res==-1LL)
cout<<"Impossible";
else
cout<<res;
}
return 0;
}