scanf("%lld",&radix);一开始没写。。%。。
找了我半天,每次都是细节。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>//里面有isdigit():数字字符返回true
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll convert(ll radix,string num){
ll result=0;
int len=num.length();
//ll q=1;
int index=0;
for(int i=len-1;i>=0;i--){//注意是从右边到左边
int temp=isdigit(num[i])?num[i]-'0':num[i]-'a'+10;
result+=temp*pow(radix,index++);
//q*=radix;
}
return result;
}
ll find(ll n1,string n2){//n1是转换为十进制的数 和n2比较
//二分查找法:考虑溢出的情况<0??
//*max_element取string n2中数值最大的数的值
char it =*max_element(n2.begin(),n2.end());
ll low=(isdigit(it)? it-'0':it-'a'+10)+1;
ll high=max(n1,low)+1;
while(low<=high){
ll mid=(high+low)/2;
ll number2=convert(mid,n2);
if(number2<0 || number2>n1){
high=mid-1;
}else if(number2==n1){
return mid;
}else{
low=mid+1;
}
}
return -1;//-1表示找不到
}
int main(){
string n1,n2;
int tag;ll radix;
cin>>n1>>n2>>tag>>radix;
//scanf("%lld",&radix);//cin和scanf不可混用
//将已知radix的数赋给N1
if(tag!=1){
n1.swap(n2);
}
ll comp1=convert(radix,n1);
ll result=find(comp1,n2);
//ll result=tag==1?find(convert(radix,n1),n2):find(convert(radix,n2),n1);
if(result==-1){
cout<<"Impossible";
}else{
printf("%lld",result);
}
return 0;
}