pat 1010. Radix (25)

来源:互联网 发布:vb mac版 编辑:程序博客网 时间:2024/05/29 03:52

https://www.patest.cn/contests/pat-a-practise/1010


代码来自http://blog.csdn.net/jtjy568805874/article/details/50782565


#include <iostream>#include <string>#include <algorithm>using namespace std;typedef unsigned long long ull;string a, b;int tag, radix, res;ull ans, l, r;ull get(char c) {if ('0' <= c && c <= '9') return c - '0';return c - 'a' + 10;}int main() {cin >> a >> b >> tag >> radix;if (tag == 2) swap(a, b);for (int i = 0; a[i]; i++) ans = ans*radix + get(a[i]);for (int i = 0; b[i]; i++) l = max(l, get(b[i]));for (l++, r = ans + 1; l <= r;) {ull mid = l + r >> 1;ull check = 0;for (int i = 0; b[i]; i++) check = check*mid + get(b[i]);if (check == ans) res = mid;if (check >= ans) r = mid - 1; else l = mid + 1;}res ? printf("%d\n", res) : printf("Impossible\n");return 0;}


0 0
原创粉丝点击