题目

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 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

题目解读

给出两个数N1,N2,给出其中一个数N1或N2的进制(输入数据中tag=1表示这这是给出的是N1的进制,tag=2表示给出的是N2的进制),为第二个数寻找合适的进制,使得 两者转换成10进制后的结果相等。输出找到的radix,如果不存在这样的进制,那就输出 ”Impossible

N1N2的长度最多为10,每个位置上的数字可以是 0-9a-z,其中 a-z数字 10-35

注意:

  • 每个数长度最多为10,每个位置大小都可以是0-35,那么int肯定是不够存的,比如这个数是zzzzzzzzzz35 ^ 9 > 2 ^ 32,我们用 long long
  • 并未从题中看出来radix的取值范围(不要自己觉得它最大是35),所以也要定义成 long long才保险。

思路分析

相信大家都能想到的是,先把给出的N1N2(假设给出的进制是N1的进制)按照给出的进制转成进制,然后从1开始测试每种进制,把N2转成进制判断是否和N1相等,如不相等就增加进制,继续转换,比较。。。

问题是,如果这个进制存在还好说,我从1开始增加,总能找到那个进制;但是如果它不存在呢?增加到无穷大程序都跑不完。。。。

所以我们必须先确定radix的上限和下限

(以第一个数进制给出,转换成十进制结果是N1,求N2的进制为例进行说明)

  • 假如N2每个位置上的数字中最大的那个是 x ,那么N2的进制最小是 x + 1,比如你某个位置是0-9,最起码得是10进制吧。
  • 那么它的进制的最大值是多少呢??那么这个进制最大为 N1

    为什么???

    假如N2只有一位数,假设为x,那么你的进制可以是 比这个数本身大的 任意数字,它所代表的值是 x * 进制 0 = x,所以除非xN1相等,否则你取啥进制都没办法。

    假如N2有两位,那它最小也就是 '1 0',代表的值是 1 * 进制 1 + 0 * 进制 0 = 进制,所以N2的大小就等于进制的大小,如果你让N2的进制=N1时,N2都不能和N1相等,那么你把进制变得更大,N2转换后必然比N1更大。当N2有更多位时就更不用说了,肯定更不可能,每差一个位置,值就差的更多

  • 所以N2进制的取值范围【N2字符串中最大的那个字符代表的值+1,N1】

确定了进制的取值范围之后,我们可以用for循环进行遍历,但是复杂度比较高,这里我们选取二分法

注意不要漏掉对溢出情况的判断,若N2在某进制下转换成十进制的结果大于N1,应该缩小右边界;但是,如果N2在某进制下转换成十进制的结果小于0,也要缩小右边界小于0说明进制太大,它转换后long long都存不下了。

    while(low <= high) {
long long mid = low + (high - low) / 2;
long long temp = convert(numStr, mid);
// temp < 0,代表得到的数字越界,temp > target都代表当前进制太大,需要调整上限
if (temp < 0 || temp > target) high = mid - 1;
// 当前进制正好
else if (temp == target) return mid;
// 当前进制太小
else low = mid + 1;
}

完整代码

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std; /**
* 给出两个数n1,n2,给出其中一个数n1及其进制radix,为第二个数寻找合适的进制使得 两者相等
*
* 首先,每个数长度最多为10,每个位置大小都可以是0-35,那么int肯定是不够存的,比如这个数是zzzzzzzzzz,35 ^ 9 > 2 ^ 32
*
* 加入这个数每个位置上的数字中最大的那个是 x ,那么这个数的【进制最小是 x + 1】,比如你要表示0-9,最起码得是10进制吧
* 那么它的进制的最大值是多少呢??其实这个是没有限制的,你可以是任意进制,所以我们用了long long来存
*
* 但在这个题中,必须要有一个合适的进制使得n2能转为n1,那么这个【进制最大为 n1】
*
* 为什么???
*
* 假如n2只有一位,那么你的进制只要比这个数字大,就可以任意取,随意,但除非你本身和n1相等,否则你取啥进制都没办法,
* 因为最后一个位置是最低位,它代表的是 进制 ^ 0 = 1
*
* 假如n2有两位,那它最小也就是 10,1 * 进制 + 0 * 进制 ^ 0 = 进制,
* 所以n2的大小就等于进制的大小,如果你让n2的进制=n1时,n2都不能和n1相等,那么你把进制变得更大,n2就更不可能转为n1
* 当n2有更多位时就更不用说了,肯定更不可能,每差一个位置,值就差的更多
*
* 所以这个进制的取值范围是 【n2字符串中最大的那个字符代表的值+1,n1】,接着利用二分法
*
*/ // 根据进制,把字符串转为实际数字
long long convert(string numStr, long long radix) {
long long res = 0;
int exp = 0; // 最低位指数为0
// auto自动判断类型,rbegin()是最后一个字符,rend()是第一个字符
for (auto it = numStr.rbegin(); it != numStr.rend(); ++it) {
// '0'-'9',注意这里要加*才能得到值
if (isdigit(*it))
res += (*it - '0') * pow(radix, exp++);
// 'a'-'z'
else
res += (*it - 'a' + 10) * pow(radix, exp++);
}
// 返回结果
return res;
} // 找到合适的进制,使得numStr在这个进制下 == target
long long findRadix(string numStr, long long target) {
// 找到字符串形式的n2中最大的那个字符,注意这里要加*才能得到值
char maxChar = *max_element(numStr.begin(), numStr.end());
// 转为数字加1,就是 进制的下限
long long low = (isdigit(maxChar) ? maxChar - '0' : maxChar - 'a' + 10) + 1;
// 进制的上限就是 n1
long long high = max(low, target);
// 二分法
while(low <= high) {
long long mid = low + (high - low) / 2;
long long temp = convert(numStr, mid);
// temp < 0,代表得到的数字越界,temp > target都代表当前进制太大,需要调整上限
if (temp < 0 || temp > target) high = mid - 1;
// 当前进制正好
else if (temp == target) return mid;
// 当前进制太小
else low = mid + 1;
}
// 没找到合适的,返回-1
return -1;
} int main() {
// 两个数,每个位置是0-9或a-z,a-z代表10-35,每个数最多长度为10,如果是35进制,最大为35 * 35 ^ 9, 大于2^32
string n1, n2;
// radix,基数(进制),tag为1代表这个基数针对于第一个数字,tag为2代表这个基数针对第2个数字
int tag;
long long radix;
cin >> n1 >> n2 >> tag >> radix;
long long res = tag == 1 ? findRadix(n2, convert(n1, radix)) : findRadix(n1, convert(n2, radix));
// 不存在这样的进制
if (res == -1)
cout << "Impossible";
else
cout << res; return 0;
}

PAT 1010 Radix (25分) radix取值无限制,二分法提高效率的更多相关文章

  1. PAT Advanced 1010 Radix(25) [⼆分法]

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  2. 1010 Radix (25 分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  3. PAT 1029 Median (25分) 有序数组合并与防坑指南

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

  4. 【PAT甲级】1010 Radix (25 分)(二分)

    题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...

  5. 1010 Radix (25 分),PTA

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...

  6. 1010 Radix (25分)

    改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...

  7. A1010 Radix (25 分)

    一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...

  8. 7-19 PAT Judge(25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  9. PAT A1075 PAT Judge (25 分)——结构体初始化,排序

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

随机推荐

  1. tensorflow1.0 构建卷积神经网络

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import os os.envi ...

  2. pytorch seq2seq模型中加入teacher_forcing机制

    在循环内加的teacher forcing机制,这种为目标确定的时候,可以这样加. 目标不确定,需要在循环外加. decoder.py 中的修改 """ 实现解码器 &q ...

  3. 微信小程序填坑---小程序支付

    因为公司刚刚重新做了网站,所以也吧公众号和小程序提上了日程,在公众号里面没有什么问题,直接按照官方文档进行代码编写.调试,然后就解决了公众号内支付的问题. 因为小程序提供了<webview> ...

  4. 解决Cannot use a scalar value as an array

    这是类型转换的问题,看看上方代码是不是先把布尔值或者0值赋给了一个变量,然后下面循环中又把这个变量当作数组用了

  5. 2019-2020-1 20199326《Linux内核原理与分析》第四周作业

    第四周学习内容 庖丁解牛Linux内核分析第三章:MenuOS的构造 Linux内核分析实验三 学到的一些知识 操作系统两大宝剑:1.中断上下文的切换--保存现场和恢复现场 2.进程上下文的切换 Li ...

  6. centos7与8的区别

    1.关于内核版本:RHEL8采用4.18.0-xRHEL7采用3.10-0-x 2 网络时间同步 RHEL8 只使用Chronyd,不支持NTP部署. RHEL7Chronyd与NTP两者都支持 3. ...

  7. Spring Cloud 系列之 Stream 消息驱动(一)

    在实际开发过程中,服务与服务之间通信经常会使用到消息中间件,消息中间件解决了应用解耦.异步处理.流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构. 不同中间件内部实现方式是不一样的,这些中间 ...

  8. vue项目中使用bpmn-为节点添加颜色

    内容概述 本系列 “vue项目中使用bpmn-xxxx” 分为五篇,均为自己使用过程中用到的实例,手工原创,目前属于陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意 ...

  9. 部署企业LNMP架构搭建bbs

    部署企业LNMP架构 1===============部署Nginx 2===============安装及部署Mysql数据库 3===============安装PHP解析环境 4======== ...

  10. nodejs实现定时爬取微博热搜

    The summer is coming " 我知道,那些夏天,就像青春一样回不来. - 宋冬野 青春是回不来了,倒是要准备渡过在西安的第三个夏天了. 废话 我发现,自己对 coding 这 ...