暴力搜会超时所以要用二分搜索
#include <bits/stdc++.h>
using namespace std;
using f=long long;
unordered_map<char,int> Jinwei;
f trans(char *s,f radix){
f result =0;
for (int i = 0; s[i] !='\0' ; ++i) {
result=result*radix+Jinwei[s[i]];
}
return result;
}
f findRadix(char *n1,char*n2,f radix){
f right=trans(n1,radix),left=-1;
f tag=trans(n1,radix);
for (int i = 0; n2[i] !='\0' ; ++i) {
left=max(left,(f)Jinwei[n2[i]]+1);
}
while (left<right){
f mid=left+(right-left)/2,k=trans(n2,mid);
if (k<0 or k>=tag)
right=mid;
else if (k<tag)
left=mid+1;
}
if (trans(n2,left)!=tag)
return -1;
return left;
}
int main(){
char N1[11],N2[11];
f tag,radix;
scanf("%s %s%lld%lld",N1,N2,&tag,&radix);
for (int i = 0; i < 36; ++i) {
Jinwei.insert({i<10?i+'0':i-10+'a',i});
}
radix=tag==1?findRadix(N1,N2,radix):findRadix(N2,N1,radix);
if (radix==-1)
printf("Impossible");
else
printf("%lld",radix);
return 0;
}