AcWing 1482. 进制
原题链接
中等
作者:
在月球_0
,
2024-10-20 14:13:53
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long LL;
int get(char c)
{
if(c <= '9') return c - '0';
return c - 'a' + 10;
}
LL cal(string n,LL radix)
{
LL ret = 0;
for(auto c : n)
{
//无解
if((double)ret * radix + get(c) > 1e16) return 1e16;
ret = ret * radix + get(c);
}
return ret;
}
int main()
{
string n1,n2;
cin >> n1 >> n2;
int tag,radix;
cin >> tag >> radix;
if(tag == 2) swap(n1,n2);
LL target = cal(n1,radix);
LL l = 0,r = target + 1;
for(auto c : n2) l = max(l,(LL) get(c) + 1);
while(l < r)
{
LL mid = (l + r) >> 1;
if(cal(n2,mid) >= target) r = mid;
else l = mid + 1;
}
if(cal(n2,l) != target) cout << "Impossible";
else cout << l;
return 0;
}