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

以下是AC的代码:

#include<cstdio>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const long long INF =(1LL<<63)-1;
int map[256];
void mapinit(){for(char c='0';c<='9';c++){map[c] = c-'0';}for(char c='a';c<='z';c++){map[c] = c-'a'+10;}
}
long long change2dec(string str,long long radix,long long higheset){int len = str.size();long long ans= 0;for(int i=0;i<len;i++){ans=ans*radix+map[str[i]];if(ans<0||ans>INF) return -1;}return ans;
}
long long findleft(string str){int len = str.size();long long ans=-1;for(int i=0;i<len;i++){if(ans<map[str[i]]) ans=map[str[i]];}return ans+1;
}
int main(void){mapinit();string n1,n2;long long tag,radix;cin>>n1>>n2>>tag>>radix;if(tag==2) swap(n1,n2);long long num1= change2dec(n1,radix,INF);long long leftmin=findleft(n2);long long rightmax=max(num1,leftmin)+1;long long left=leftmin,right=rightmax,mid,num2;bool fnd=false;//   printf("\n--%lld--%lld--%lld--\n",leftmin,rightmax,mid);while(left<=right){mid=(left+right)/2;num2=change2dec(n2,mid,rightmax);if(num1==num2){fnd=true;break;}else if(num1<num2||num2<0){right=mid-1;}else left=mid+1;}if(!fnd) printf("Impossible");else printf("%lld",mid);return 0;
}

思路:
这道题看起来不难,实际上令人吐血(其实我连续这几天没有发记录就是因为打开书就被这道题劝退了).
题意简单来说,就是让你找 让两个数字相等的进制 (radix) 。如果你马上开始用暴力枚举,会有测试点无法通过;同时你看着这道题 多达19 个的测试点,可能心里还不知道自己面对的是什么。
然后你于是灵机一动,想用二分法来做。如何确认二分法的上下界呢? 这里又有需要注意的地方,首先可能的下界不难,将 n2 中出现过最大的字符 +1就是可能的最小下界。
上界我开始以为是36,因为 a - z 代表 10 - 35,所以可以表示的最大进制应该是36.(这个思路显然是有问题的,因为 36 以上的进制 n2也能表示出来,比如100 在99进制下代表99^ 2)。果然很多测试点都通不过,只有几分。
实际上这里,测试点中的,最大的进制可能是 int的边界最大值,也就是 2^ 32-1. 这个数量级的进制肯定会让转化为10进制过程中的数字溢出了。所以这里必须要对溢出的情况进行处理。首相定义了long long INF = (1LL<<63)-1; 代表long long 类型下的最大值,超过这个值就会变成负数。所以溢出可以表达为: num<0 || num>INF.
此外还应该注意,如果num2转化出来溢出了,可能是num2 现在的进制太小了,所以 也应该归类到 right= mid-1;的情况中。具体可以参考上面的代码。

PAT A1010 Radix (25 分)相关推荐

  1. PAT 甲级 A1010 Radix (25 分)

    题目传送门 这个题用二分做,我自己写的二分呢太菜了,只能拿到19分,不放出来丢人了.后面看了yxc的代码,美妙绝伦哈. 慢慢来拜读一下. #include "bits/stdc++.h&qu ...

  2. PAT A1010 Radix

    原文链接: PAT A1010 Radix 上一篇: 完美数列 PAT A1085 下一篇: PAT A1044. Shopping in Mars (25) https://www.patest.c ...

  3. PAT A1010.Radix 二分法

    PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...

  4. 7-227 PAT排名汇总 (25 分)

    7-227 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设 ...

  5. 1085 PAT单位排行 (25分)-PAT乙级真题-C++实现

    1085 PAT单位排行 (25分)-PAT乙级真题-C++实现 题目描述: 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整 ...

  6. 1095 解码PAT准考证 (25分)击破测试点3、4,50ms内通关

    1095 解码PAT准考证 (25分)测试点34用时低于35ms 前言 一.题目简介 二.原题内容 1. 设定 2. 输入格式 3. 输出格式 三.题目分析 1. 要求1分析 2. 要求2分析 3. ...

  7. PAT A1010 Radix +神

    1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 1 ...

  8. 【PAT - 甲级1010】Radix (25分)(二分,进制转化)

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

  9. PAT Advanced—1010 Radix (25分)

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

最新文章

  1. 一维数组用户输入数据求平均值
  2. Ubuntu下VIM使用指南
  3. extern 定义_Essential Cython - 2.7 - 定义并使用模板函数
  4. linux怎么对端口限速,linux – 如何使用iptables对SSH连接进行速率限制?
  5. ubuntu20.1 查看apt仓库_上海食品冷藏仓库出租查看
  6. java 谓词_谓词::不适合Java
  7. 每日一题:leetcode173.二叉搜索树迭代器
  8. 买无线路由还是买无线AP?
  9. 10年10亿才能烧好电子商务?
  10. 转:理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
  11. 基于Java的企业新闻管理系统的设计与实现
  12. 原生js格式化显示json对象以及stringify的妙用
  13. wow 卡正在连接服务器,魔兽世界怀旧服卡蓝条服务器无法连接解决办法
  14. photoshop 前端切图基础教程
  15. 【听课笔记】复旦大学遗传学_08遗传分析方法
  16. 进军AR领域,这一次罗永浩能成吗?
  17. 一种很轻松的Excel关键字方式进行网页Web自动化测试(Java+Selenium+TestNG+Excel)
  18. A/Btest:组间的差异性检验,统计功效以及反选样本量,附python底层实现代码
  19. 安装vs无法连接网络
  20. <数据库> if 条件语句的使用 SQL26 计算25岁以上和以下的用户数量

热门文章

  1. 对于addEventListener()事件的看法
  2. Golang开发环境搭建
  3. matlab实现彩色图像的直方图均衡化
  4. Windows远程桌面卡顿问题(包含网络调优)
  5. JSP基本语法、3个编译指令、7个动作指令、9个内置对象
  6. 机器学习3:——Pandas——3:文件读取和存储
  7. Spring 实战(第 5 版)
  8. 珍藏5个在线免费接收国内外手机短信验证码的网络服务
  9. 大厂经典高频面试题体系化集合,最强技术实现
  10. 数据脱敏为何如此重要?