#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<string>
using namespace std;
vector<int> v1;
vector<int> v2;
long long int getval(int which, long long int r)
{
long long int res = 0;
if (which == 1)
{
for (int i = 0; i < v1.size(); ++i)
res += (v1[i] * pow(r, v1.size() - i - 1));
}
else {
for (int i = 0; i <v2.size(); ++i)
res += (v2[i] * pow(r, v2.size() - i - 1));
}
return res;
}
int main()
{
//freopen("test.txt", "r", stdin);
int t, r, min1 = 999999, min2 = 999999;
long long int n1 = 0;
string a, b;
cin >> a >> b >> t >> r;
for (int i = 0; i < a.length(); ++i) {
if (a[i] >= '0'&&a[i] <= '9') {
v1.push_back(a[i] - '0');
if (a[i] - '0' < min1)min1 = a[i] - '0';
}
else if (a[i] >= 'a'&&a[i] <= 'z') {
v1.push_back(a[i] - 'a' + 10);
if (a[i] - 'a' + 10 < min1)min1 = a[i] - 'a' + 10;
}
}
for (int i = 0; i < b.length(); ++i)
{
if (b[i] >= '0'&&b[i] <= '9') {
v2.push_back(b[i] - '0');
if (b[i] - '0' < min2)min2 = b[i] - '0';
}
else if (b[i] >= 'a'&&b[i] <= 'z') {
v2.push_back(b[i] - 'a' + 10);
if (b[i] - 'a' + 10 < min2)min2 = b[i] - 'a' + 10;
}
}
if (t == 2) {
for (int i = 0; i < v2.size(); ++i)
n1 += (v2[i] * pow(r, v2.size() - i - 1));
long long int l = min1 + 1;
long long int r = max(n1, l);
while (l <= r)
{
long long int mid = (l + r) / 2;
long long int m = getval(1, mid);
if (m<0 || m > n1)r = mid - 1;
else if (m == n1) {
cout << mid;
return 0;
}
else l = mid + 1;
}
}
else {
for (int i = 0; i < v1.size(); ++i)
n1 += (v1[i] * pow(r, v1.size() - i - 1));
long long int l = min2 + 1;
long long int r = max(n1, l);
while (l <= r)
{
long long int mid = (l + r) / 2;
long long int m = getval(2, mid);
if (m<0 || m > n1)r = mid - 1;
else if (m == n1) {
cout << mid;
return 0;
}
else l = mid + 1;
}
}
cout << "Impossible";
return 0;
}
今天非常无语。这题第一次没过,第二次还是没过,实在不知道最后一个测试点为什么过不了,但是还是写下总结:
long long int 类型的变量加的时候可能会溢出,把符号位变成1,所以判断现在在试的radix时候如果太大了可能会小于0
要是大佬们有空的话,能帮我改改吗,我没有c币