PAT (Advanced Level) Practice-1010 Radix

Posted dululu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) Practice-1010 Radix相关的知识,希望对你有一定的参考价值。

基数上限为给定的那个数(不懂),需要二分查找?‘?‘?

 

#include<bits/stdc++.h>

typedef long long ll;
int n, tag, radix;
char num[2][15];

#define val(c) (‘0‘<=(c)&&(c)<=‘9‘?(c)-‘0‘:(c)-‘a‘+10)

ll cal(char* s, ll r) {
    ll ret = 0;
    for (int i = 0; s[i]; ++i) 
        (ret *= r) += (ll) val(s[i]);
    return ret;
}
inline int max(int a, int b) { return a > b ? a : b; }
int main() {
    scanf("%s%s%d%d", num[0], num[1], &tag, &radix); --tag;
    ll target = cal(num[tag], radix), lo = 2, hi = target; // 最小2进制
    for (int i = 0; num[1-tag][i]; ++i) lo = max(lo, val(num[1-tag][i]) + 1);
    while (lo < hi) {
        ll mi = lo + ((hi - lo) >> 1), t = cal(num[1-tag], mi);
        (t < 0 || t >= target) ? hi = mi : lo = mi + 1;
    }
    if (target == cal(num[1-tag], lo)) printf("%lld
", lo);
    else printf("Impossible
");
    return 0;
}

 

以上是关于PAT (Advanced Level) Practice-1010 Radix的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038

pat advanced level 1063

PAT Advanced level 1003 Emergency