#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
long long convert(string n, long long radix)
{
long long sum = 0, temp = 0, idx = 0;
for (auto it = n.rbegin(); it != n.rend(); it++)
{
temp = isdigit(*it) ? *it - '0' : *it - 'a' + 10;
sum += temp * pow(radix, idx++);
}
return sum;
}
long long find_radix(string n, long long sum)
{
char it = *max_element(n.begin(), n.end());
long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
long long high = max(low, sum);
while (low <= high)
{
long long mid = (low + high) >> 1;
long long temp = convert(n, mid);
if (temp == sum)
return mid;
else if (temp > sum || temp < 0)
{
high = mid - 1;
}
else
low = mid + 1;
}
return -1;
}
int main()
{
string n1, n2;
long long tag, radix;
cin >> n1 >> n2 >> tag >> radix;
int ans = tag == 1 ? find_radix(n2, convert(n1, radix)) : find_radix(n1, convert(n2, radix));
if (ans == -1)
{
cout << "Impossible";
}
else
cout << ans;
}
solution#include <iostream>#include <cstring>#include <cmath>#include <algorithm>using namespace std;long long convert(string n, long long radix){ long long sum = 0, temp = 0, idx = 0; for (auto it = n.rbegin(); it !=