mid也要为long long!!!
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100010;
long long a1 = 0, b = 0;
int ctoint(char c) {
{
if (c >= '0' && c <= '9')
return c - '0';
return c - 'a' + 10;
}
}
long long ctol(char a[], int radix) {
long long re = 0; long long tm = 1;
if (radix==0)
{
return 0;
}
for (int i = 0; i < strlen(a); i++)
{
int x=ctoint(a[i]);
tm = re * radix;
re =tm + x;
if (re < 0||tm<0) //需要判断这个,也许进制太大了呢
return -1;
}
return re;
}
int binarySearch(int l, int r, char a[]) {
long long temp;
if (strlen(a) == 1)return max(ctoint(a[0]) + 1,2);
while (l<=r)
{ int mid = (l + r) >>1;
temp = ctol(a, mid);
if (temp>=a1||temp==-1)
{
r = mid-1;
}
else
{
l = mid+1;
}
}
return l;
}
int main() {
//while (1) {
char n1[maxn], n2[maxn];
long long tag, radix, ans,maxr=2;
cin >> n1 >> n2 >> tag >> radix;
if (tag == 2)
{
swap(n1, n2);
}
a1 = ctol(n1, radix);
for (int i = 0; i < strlen(n2); i++)
{
if (ctoint(n2[i]) + 1 > maxr)maxr = ctoint(n2[i]) + 1;
}
ans = binarySearch(maxr,a1+1,n2);
//cout << ans<<' '<<ctol(n2,ans)<<' '<<ctol(n2,20);
if (a1 != ctol(n2, ans) || ans == -1) {
cout << "Impossible";
}
else
cout << ans;
//}
return 0;
}