PAT 1010 Radix (二分)

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.

思路

输出可以使得N1=N2的最小进制。

因为此题的进制上限是正无穷,因此考虑使用二分法来挑选最佳进制。因此,问题可以转化为如何选择进制的最大值与最小值。

小技巧

? ①如果flag=2,那么swap(a,b )

? ②用string来存字符串,统一将字符转换为对应的整数,方便运算,见代码中的init()函数(如果用char[]来存,就不能这么操作了,因为‘0‘-‘0‘ = ‘\0‘,strlen函数会出问题)。

这个题有比较坑的地方

? ①long long类型有可能溢出。这就造成了如果N1的范围大于了long long的范围,有可能会编程负数。我看一些博客中,简单的加了一个判断条件N2 < 0。但是面对一个超范围的数字,应该不能简单的判断N1与N2的大小(例如N2 = -1,但有可能是LLONG_MAX + LLONG_MAX + 1;而N1 = 8,但有可能是LLONG_MAX + LLONG_MAX + 9,这个时候通过N2<0,来作为N2>N1的判断条件是不对的)。好在这题的数据没有考虑的这个问题。

? ②一定要注意进制的最大值是无穷,而不是35。但是最大值是可以确定的。设N1对应的十进制为num,那么N2的最大进制不超过num。例如N2为110,第二位为1,而其他位不为0,已经表示此数比num要大了,因此进制偏大;例如N2为h,第二位为0,这时,用num的值来作为最大进制是可以挑选出的。但是要保证最大进制大于最小进制,因此代码中有个max操作。

代码

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <limits.h>
using namespace std;
string a, b;
long long flag, radex;
long long minn = INT_MIN, maxx = INT_MIN;

long long to10(string s, long long radex){
    long long p = 1;
    long long sum = 0;
    for(int i = s.length() - 1; i >= 0; i--){
        sum = sum + p * s[i];
        p *= radex;
    }
    return sum;
}

void prove(long long dec){
    while(minn <= maxx){
        long long mid = (minn + maxx) >> 1;
        long long num = to10(b, mid);
        if(num < 0 || num > dec){
            maxx = mid - 1;
        }
        else if(num == dec){
            cout << mid;
            return;
        }
        else{
            minn = mid + 1;
        }
    }
    cout << "Impossible";
}

void init(){
    for(int i = 0; i < a.length(); i++){
        if(isdigit(a[i]))   a[i] = a[i] - '0';
        else    a[i] = a[i] - 'a' + 10;
    }
    for(int i = 0; i < b.length(); i++){
        if(isdigit(b[i]))   b[i] = b[i] - '0';
        else    b[i] = b[i] - 'a' + 10;
    }
}

int main() {
    cin >> a >> b >> flag >> radex;
    if(flag == 2)       swap(a, b);
    init();
    minn = *max_element(b.begin(), b.begin()) + 1;
    long long dec = to10(a, radex);
    maxx = max(dec, minn);
    prove(dec);

    return 0;
}

原文地址:https://www.cnblogs.com/woxiaosade/p/12335228.html

时间: 2024-08-09 02:42:02

PAT 1010 Radix (二分)的相关文章

PAT 1010 Radix

过了一天感觉没干什么,想刷一发题弥补一下,正在考虑去做哪道,室友说去试试PAT 1010,果然当年自己直接跳过了这题,一看这通过率(0.07)有点夸张了.题目是已知一个数,求另外一个数的基数,使得这两个数数值上相等.很自然的考虑到使用二分搜索来确定这个基数,数字表示使用[0-9a-z],这tmd的让人很容易的想到基数的范围就在1~36之间了,艹,基数是可以超过这个范围的,如果没有考虑到这一点,可以得到的一个典型分值就是19分.不过基数在[1, 36]之间的话这个搜索范围太小了,直接暴力遍历也可以

PAT 1010. Radix (25)

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 i

PAT 1010 Radix 进制转换+二分法

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

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 b

1010 Radix(25 分)

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 N?1?? and N?2??, your task is

PAT 1010

1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

1010 Radix

1010 Radix 注意点 如111 1 1 10类似情况下,若n为个位数,如果本身比另一个数小,则多少的进制都是没有用的(可能会造成空循环而超时),不过好像没有这么一个测试用例 进制应该比最少数据中的最大的数要大一,如8最少是9进制的数,z最少是36进制的数 注意算法中的超时问题如9999999999 11 1 10很容易超时,注意匹配的算法 如果用c等强类型语言写,注意溢出问题,int类型肯定是不够的 python3代码 def getNum(num, radix): sum = 0 co

PAT 甲级测试题目 -- 1010 Radix

题目链接 题目描述 给你两个数以及其中一个数的基数(进制数),找出另一个数的基数,找不到就输出 Impassible 分析 思路不是很难,基本可以用进制转换加循环判断做,但是有坑... 坑1:上界不是36....上界是确定的那个数的十进制加 1 . 坑2:暴力循环会导致时间超限,用二分法解决 坑3:二分过程中会有数据溢出,得到的负数处理方式和找到较大数处理方式一样,因为溢出的数和题目条件不符,所以可以舍弃. 实现 #include<iostream> #include<string.h&

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 rad