#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
LL digit(char c)
{
if(c>='0'&&c<='9')
return c-'0';
else if(c>='a'&&c<='z')
return c-'a'+10;
}
LL changeto10(int radix,char str[])
{
LL sum=0;
int len=strlen(str);
for(int i=0;i<len;i++)
{
sum=(LL)radix*sum+digit(str[i]);
}
if(sum<0)
return -1;
return sum;
}
LL minradix(char str[])
{
LL max=-1,len=strlen(str);
for(LL i=0;i<len;i++)
{
LL temp=digit(str[i]);
if(temp>max)
max=temp;
}
return max+1;
}
void judge(char str[],LL sum1)
{
LL low=minradix(str),high=sum1+1,mid;
while(low<high)
{
mid=(low+high)/2;
LL sum2=changeto10(mid,str);
if(sum2==-1||sum2>=sum1)
{
high=mid;
}
else
{
low=mid+1;
}
}
if(sum1==changeto10(high,str))
printf("%lld",high);
else
printf("Impossible");
}
int main()
{
char str1[20],str2[20];
int tag,radix;
scanf("%s%s%d%d",str1,str2,&tag,&radix);
char str3[20];
if(tag==2)
{
strcpy(str3,str2);
strcpy(str2,str1);
strcpy(str1,str3);
}
LL temp=changeto10(radix,str1);
judge(str2,temp);
return 0;
}
其中low与high的取值
low为N2中最大的位数+1(因为如果直接0最小进位)
high为N1的数+1