#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
LL MAX = (1LL << 63) - 1;
char n1[11], n2[11];
int tag, r;
LL map[256];
void init(){
for(char c='0'; c<='9'; c++){
map[c] = c - '0';
}
for(char c='a'; c<='z'; c++){
map[c] = c - 'a' + 10;
}
}
LL turn(char n[], LL r, LL t){
LL sum = 0;
int len = strlen(n);
LL ans = 1;
for(int i=len-1; i>=0; i--){
sum += ans * map[n[i]];
ans *= r;
if(sum<0 || sum>t) return -1;
}
return sum;
}
int findlow(char n[]){
int ans = -1;
int len = strlen(n);
for(int i=0; i<len; i++){
if(map[n[i]] > ans){
ans = map[n[i]];
}
}
return ans + 1;
}
int cmp(char n[], LL mid, LL t){
LL num = turn(n, mid, t);
if(num == -1) return -1;
if(num == t) return 1;
return 0;
}
LL solve(char n[], LL low, LL high, LL t){
LL mid;
while(low <= high){
mid = (low+high) / 2;
int flag = cmp(n, mid, t);
if(flag == 1){
return mid;
}else if(flag == -1){
high = mid - 1;
}else if(flag == 0){
low = mid + 1;
}
}
return -1;
}
int main(){
init();
scanf("%s %s %d %d", n1, n2, &tag, &r);
if(tag == 2){
swap(n1,n2);
}
LL t = turn(n1, r, MAX);
LL low = findlow(n2);
LL high = max(t, low) + 1;
LL ans = solve(n2, low, high, t);
if(ans == -1){
printf("Impossible");
}else{
printf("%lld", ans);
}
}
这题写之前看了一下通过率0.11就感觉到了不对劲,但是看完题目又感觉挺简单了,就按自己思路写了。
样例测试也都没问题,交上去以后惊了,19个测试点!!!刷PAT还是第一次遇到这么多测试点。分数15/25,不过这题这么低的通过率这分数我已经满足了。
看了一下书上才发现原来这题的坑这么多,是我想的少了,按书上的思路自己又写了一遍。