1010 Radix

1010 Radix

注意点

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

python3代码

def getNum(num, radix):
    sum = 0
    count = 0
    for i in num[::-1]:
        if i <= '9':
            sum += int(i)*pow(radix, count)
        else:
            sum += (ord(i)-87)*pow(radix, count)
        count += 1
    return sum

num0 = 0
num1 = 0
data_list = input().split(" ")

if data_list[2] == '1':
    num0 = getNum(data_list[0], int(data_list[3]))
    num1 = data_list[1]
else:
    num0 = getNum(data_list[1], int(data_list[3]))
    num1 = data_list[0]

if len(num1) == 1 and getNum(num1, 36) < num0:
    print("Impossible")
    exit()

max_c = max(num1)
if max_c > '9':
    max_c = ord(max_c) - 87
else:
    max_c = int(max_c)

radix = max_c + 1
result = getNum(num1, radix)

basic = 5
while result < num0:
    radix *= basic
    result = getNum(num1, radix)
    if result > num0:
        radix //= basic
        basic = radix // 2
        result = getNum(num1, radix)
        while result < num0:
            if basic == 0:
                break
            radix += basic
            result = getNum(num1, radix)
            if result > num0:
                radix -= basic
                result = getNum(num1, radix)
                basic //= 2

if result == num0:
    print(radix)
else:
    print("Impossible")

原文地址:https://www.cnblogs.com/d-i-p/p/10701125.html

时间: 2024-10-28 10:15:09

1010 Radix的相关文章

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

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

1010. Radix (25)

简直是哭着做出来的,起码提交了22遍.创纪录了... 首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了... 考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找. 注意,进制数不能小于数中最大的数字. 如果将两个数字作为字符串输入,注意排除字符串前面的0. 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,

PAT 1010 Radix

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

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

PAT 甲级测试题目 -- 1010 Radix

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

PAT 甲级 1010 Radix

https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 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 fo

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