1010 Radix (25分)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:\ N1 N2 tag radix\ Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

思路:

一开始低估这道题目了,还觉得0.11的正确率的题目也不过如此,结果发现这题目是真的坑,自己太大意了。
出看题目很多人会根据{ 0-9, a-z }以为待求数的基数是不会大于36的,但是题目实际上的意思是待求数基数可以任意取。所以会面临的问题是:
1.基数的范围是多少?
2.怎么样缩小基数的范围,找到最终的进制值?
3.基数过大最后转化十进制超long long了怎么办?

首先已知进制的数的十进制是不会超long long的(不然这题解有无数个了),所以基本思路还是求出已知数的十进制,然后找一个基数可以让待求数与之相等。
基数的下限是待求数的所有digit的最大值+1,这个毫无疑问,上限则是已知数的十进制。因为待求数的基数如果比已知数的十进制还大,那么待求数只能是一位,这是不可能的。

比方说现在已知数十进制是35,待求数基数>35,那么待求数只能是z,这样的话z的基数可以有无数个,这不可能。再比方说已知数十进制是1000,待求数基数>1000,然而一位待求数最大值只能是z(即35),这样也不可能。

所以待求数的基数范围就被限制在max{digit}+1 : decimal(N1).

遍历这个范围会超时,因此采用二分搜索
如果说在该基数下待求数十进制超longlong 了,那么根据计算机系统的知识,带符号数为补码存储,超过存储上限值会变为负数,这个时候也说明基数选的太大了。

源码:

#include<bits/stdc++.h>

using namespace std;

int main() {
    string a, b, t;
    int tag, radix;
    cin >> a >> b >> tag >> radix;
    if (tag == 2) {
        t = a;
        a = b;
        b = t;
    }
    long long de = 0;
    int len = a.length();
    for (int i = len - 1; i >= 0; i--) {
        long long temp;
        temp = isdigit(a[i]) ? a[i] - '0' : a[i] - 'a' + 10;
        de += temp * pow(radix, len - 1 - i);
    }

    long long low, high;//分别表示b的进制的上下限
    char c = *max_element(b.begin(), b.end());//找出最大的元素
    low = (isdigit(c) ? c - '0' : c - 'a' + 10) + 1;//low即为最大元素+1
    high = max(low, de);//high即为已知数十进制和low中较大者
    int f = 0;
    long long r;
    while (low <= high) {
        long long mid = (low + high) / 2;
        long long tar = 0;
        for (int j = b.length() - 1; j >= 0; j--) {
            long long temp;
            temp = isdigit(b[j]) ? b[j] - '0' : b[j] - 'a' + 10;
            tar += temp * pow(mid, b.length() - 1 - j);
        }
        if (tar > de || tar < 0)high = mid - 1;//注意如果计算的结果是负数说明超过了long long范围,还是说明基数过大了
        else if (tar < de)low = mid + 1;
        else {
            f = 1;
            r = mid;
            break;
        }
    }
    if (!f)
        cout << "Impossible" << endl;
    else
        cout << r << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值