#include <cstdio>
#include <cctype>
#include <string>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll convert(string s,ll radix){
ll sum=0;
int index=0,temp=0;
for (auto i = s.rbegin(); i!=s.rend() ; ++i) {
temp = isdigit(*i)? *i-'0': *i-'a'+10;
sum +=temp*pow(radix,index++);
}
return sum;
}
ll find_radix(string n,ll num){
char it = *max_element(n.begin(),n.end());
ll low = (isdigit(it) ? it-'0':it-'a'+10)+1;
ll high =max(low,num);
while (low<=high){
ll mid = (low+high)/2;
ll t = convert(n,mid);
if(t<0 ||t>num) high = mid - 1;
else if(t ==num) return mid;
else low = mid+1;
}
return -1;
}
int main(){
string s1,s2;
cin>>s1>>s2;
ll tag,radix;
cin>>tag>>radix;
ll temp,ans;
if(tag==1){
temp = convert(s1,radix);
ans = find_radix(s2,temp);
} else{
temp = convert(s2,radix);
ans = find_radix(s1,temp);
}
if(ans==-1){
printf("Impossible");
} else{
cout<<ans;
}
return 0;
}
1010甲级进制转换
最新推荐文章于 2020-08-25 23:05:36 发布