二分思想寻找进制
long long 溢出的话返回1
有些坑点
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll targetnum(const string &s,ll radix)
{
int len=s.length();
ll result=0;
ll tmp;
for(int i=0; i<len; i++)
{
if(s[i]>='0'&&s[i]<='9')
{
tmp=s[i]-'0';
}
else if(s[i]>='a'&&s[i]<='z')
{
tmp=s[i]-'a'+10;
}
result=result*radix+tmp;
if(result<0)
return -1;
}
return result;
}
int main()
{
string s1,s2;
int tag;
long long radix;
int result;
cin>>s1>>s2>>tag>>radix;
if(tag==2)
swap(s1,s2);
ll target=targetnum(s1,radix);
ll Min=2;
ll Max=target+1;
for(int i=0; i<s2.length(); i++)
{
if(s2[i]>='0'&&s2[i]<='9')
Min=max((ll)s2[i]-'0'+1,Min);
else
Min=max((ll)s2[i]-'a'+11,Min);
}
ll mid,tmp;
while(Min<=Max)
{
mid=(Min+Max)>>1;
tmp=targetnum(s2,mid);
if(tmp==-1||tmp>target)
{
Max=mid-1;
}
else if(tmp<target)
{
Min=mid+1;
}
else
{
cout<<mid<<endl;
return 0;
}
}
cout<<"Impossible"<<endl;
return 0;
}