标签:while pat eve scan ble color lin printf %s
基数上限为给定的那个数(不懂),需要二分查找?‘?‘?
#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\n", lo); else printf("Impossible\n"); return 0; }
PAT (Advanced Level) Practice-1010 Radix
标签:while pat eve scan ble color lin printf %s
原文地址:https://www.cnblogs.com/dululu/p/13367675.html