原题:
源代码:
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
string n1, n2;
long long tag, radix;
long long resradix = -1;
long long to10(string s, long long r){
long long res = 0;
for(int i = s.length() - 1; i >= 0; i --){
long long t;
if(s[i] >= '0' && s[i] <= '9')
t = s[i] - '0';
else
t = s[i] - 'a' + 10;
if(t >= r){
res = -1;
break;
}
res += (pow(r, s.length() - 1 - i) * t);
}
return res;
}
bool isequal(string s, long long n){
char it = *max_element(s.begin(), s.end());
long long left = (isdigit(it) ? it - '0': it - 'a' + 10) + 1;
long long right = max(n, left);
long long t;
while(left <= right){
long long mid = (left + right) / 2;
t = to10(s, mid);
if(t > n || t < 0)
right = mid - 1;
else if(t == n){
resradix = mid;
right = mid - 1;
}else{
left = mid + 1;
}
}
if(resradix != -1)
return true;
return false;
}
int main(){
cin>>n1>>n2>>tag>>radix;
if(tag == 2) swap(n1, n2);
long long resn1 = to10(n1, radix);
if(isequal(n2, resn1))
cout<<resradix;
else
cout<<"Impossible";
}
已AC:
易失分点:
1.数据很大,要用long long
2.规模很大,要用二分法才能过样例7
3.二分端点要从给定字符串中最大的值开始,到