PAT Advanced 1010 Radix

题目

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

理解与算法

题目不是特别复杂,难的是测试用例范围,测试用例可能非常非常大,超出int的范围,甚至会超出long long int的范围,变成负数,所以编程的时候要考虑到这点。

还要确定进制的上限与下限,最小进制应该比这个数中最大的那位数字大1,最大进制应该不超过前一个已经确定好进制的数。考虑到数的范围,我们全部用long long int来实现。

char it = *max_element(N2.begin(), N2.end());
long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
long long high = max(num, low);

写这道题目的时候要想想如何优化,如果每一个进制都试一遍那肯定会超时,考虑到进制越大,数的值肯定会变大,而我们已经确定好数的进制的范围,相当于进制与数值是一个递增函数,所以我们就能用二分法来加快搜索速度!

while (low <= high) {
    long long mid = (low + high) / 2;
    long long t = to_decimal(N2, mid);
    if (t < 0 || t > num) high = mid - 1;
    else if (t == num) return mid;
    else low = mid + 1;
}

代码与实现

#include <iostream>
#include <algorithm>
#include <cctype>
#include <cmath>

using namespace std;

long long int to_decimal(string num, long long int radix) {
    long long int sum = 0, index = 0, tmp;
    for (long long int i = num.length() - 1; i >= 0; --i) {
        tmp = isalpha(num[i]) ? (num[i] - 'a' + 10) : (num[i] - '0');
        sum += tmp * pow(radix, index++);
    }
    return sum;
}

long long find_radix(long long int num, string N2) {
    char it = *max_element(N2.begin(), N2.end());
    long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
    long long high = max(num, low);
    while (low <= high) {
        long long mid = (low + high) / 2;
        long long t = to_decimal(N2, mid);
        if (t < 0 || t > num) high = mid - 1;
        else if (t == num) return mid;
        else low = mid + 1;
    }
    return -1;
}

int main() {
    string N1, N2, tmp;
    long long int flag, radix;
    cin >> N1 >> N2 >> flag >> radix;
    // 交换N1与N2
    if (flag == 2)
        swap(N1, N2);

    long long int result = find_radix(to_decimal(N1, radix), N2);
    if (result > 0)
        cout << result;
    else
        cout << "Impossible";
    return 0;
}
posted @ 2021-01-24 16:54  tanknee  阅读(37)  评论(0编辑  收藏  举报