1010 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​1and N​2​​ , your task is to find the radix of one number while that of the other is given.

输入格式

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2each 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.

输出格式

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

总结

  1. 使用二分法不会超时,但二分的上下界得确定。二分的下界low为未知进制的所有数位中的最大数加1,上界为max(low,N1)+1。二分的下界如果简单确定为2,会有两个测试点过不去。如果定为0,会有更多点过不去,佛了。
  2. 进制转化时,long long类型的数照样会发生上溢,因此也要把这种情况加入二分的判断中
  3. 输出的进制需要是尽可能小的进制,因此每次求到一个解都需要比较
  4. 还有一点值得提,当N1和N2都越界时,应该输出radix,但是测试数据里面貌似没有这种情况,为了代码看起来比较少我就判断这种情况。

AC代码

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
using namespace std;
typedef long long ll;
ll toNum(char c) {if (isdigit(c)) {return c - '0';}return c - 'a' + 10;
}
ll toDemical(string a, int r) {ll ans = 0;for (int i = 0; i < a.size(); i++) {  ans = ans * r + toNum(a[i]);if (ans < 0) return -1; //越界}return ans;
}
int main() {  string a, b;int tag, radix;ll N1, N2;cin >> a >> b >> tag >> radix;if (tag == 2) {  //a为已经确定进制的数swap(a, b);}N1 = toDemical(a, radix);  //转化为10进制ll low = 2, high = max(low, N1) + 1, ans;for (int i = 0; i < b.size(); i++)        //算出最低的进制数 {low = max(low, toNum(b[i]) + 1);}bool flag = false;while (low <= high) {   //二分ll mid = (low + high) / 2;N2 = toDemical(b, mid);if (N2 == -1|| N2 > N1||(int)mid<0) { //过大,或者mid超出int的范围(不过测试数据并没有这种情况)high = mid - 1;}else if (N2 < N1) {low = mid + 1;}else {  //有解,继续查询更小的解flag = true;ans = mid;high = mid - 1;}}if (flag) {printf("%lld\n", ans);}else {printf("Impossible\n");}return 0;
}

PAT 甲级 A1010相关推荐

  1. PAT 甲级 A1010 Radix (25 分)

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

  2. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

  3. PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy

    PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy 通过:643 提交:1220 通过率:52% Eva would like to make a ...

  4. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

  5. Pat甲级 1002 A+B for Polynomials

    Pat甲级 1002 A+B for Polynomials 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems ...

  6. Pat甲级 1001 A+B Format

    Pat甲级 1001 A+B Format 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems/99480552 ...

  7. PAT甲级1055 The World‘s Richest:[C++题解]k路归并

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 采用二维数组vector[N]来存每个年龄的人(结构体),然后分别从大到小排序.剩下的任务就是从给定的年龄[a ,b]中,k路归并最 ...

  8. PAT甲级1051 Pop Sequence:[C++题解]模拟栈、判断序列是否是合法的出栈序列

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 题意:将1~N压栈,判断给定序列是否是合法的出栈序列. 对于序列1~N中的每个值i,先将其压入栈.然后对于它就有两种处理方法:要么压 ...

  9. PAT甲级1085 Perfect Sequence :[C++题解]双指针

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:求满足条件M≤m×pM \leq m\times pM≤m×p的区间[m, M]最长是多少.此处有一性质:当最大值M变大的时候,最小值 ...

最新文章

  1. SQLServer CDC数据迁移和数据抽取功能介绍 2
  2. JVM:内存划分总结
  3. JUnit中@Test的运行顺序
  4. table合并单元格宽度自适应
  5. 技术人生第5篇——浅谈如何成为技术一号位?
  6. android 设备唯一码的获取,Cpu号,Mac地址
  7. Axure RP8下载以及注册
  8. JavaScript学习手册一:JS简介
  9. 2022年工业互联网厂商全景地图
  10. 搜狗语音云开发入门(二)——使用离线语音识别服务
  11. 用万用表判断三极管极性
  12. pytorch中dim的含义及相关做法
  13. 星巴克中国门店全职员工涨至14薪;迪桑特在北京三里屯开设全球最大零售门店|美通社头条...
  14. php mysql 排班表_PHP倒班五班三倒
  15. maven java_home
  16. 数据库SQL实战题目详解(全61题)---(41-61)部分
  17. APP在线抢答解决方案(RTC直播间抢答或者抢背唱歌)
  18. 地图数据赋能ADAS的探索与实践(转载)
  19. 画论51 沈灏《画尘》
  20. 谁来给移动互联网发牌照:安卓生态圈畸形发展

热门文章

  1. java毕业设计_短视频分享网站
  2. nose-report
  3. javase笔记基础篇
  4. gke下载_我们如何在GKE上升级Kubernetes
  5. 正确数据确保数据完整性
  6. Mahout学习总结
  7. Rk3588 Rk3588s对比
  8. RecyclerView添加头部
  9. 初始java ~ 逻辑控制
  10. 高仙技术家|SLAM(二)——完善回环检测的视觉SLAM