A1010. 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.
Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<limits.h>
#include<string.h>
using namespace std;
long long str2num(char s[], long long radix){
long long ans = , bit, P = ;
for(int i = strlen(s) - ; i >= ; i--){
bit = (s[i] >= '' && s[i] <= '' ) ? (s[i] - '') : (s[i] - 'a' + );
ans = ans + P * bit;
if(ans < )
return -;
P = P * radix;
}
return ans;
}
long long binSearch(long long low, long long high, char s[], long long x){
long long mid, temp;
while(low < high){
mid = low + (high - low) / ;
temp = str2num(s, mid);
if(temp > && temp >= x || temp < )
high = mid;
else low = mid + ;
}
return low;
}
int findMax(char s[]){
int max = -, temp;
for(int i = ; s[i] != '\0'; i++){
temp = (s[i] >= '' && s[i] <= '' ) ? (s[i] - '') : (s[i] - 'a' + );
if(temp > max)
max = temp;
}
return max;
}
int main(){
char N1[], N2[], temp[];
long long radix, Na, Nb, re;
int tag;
scanf("%s %s %d %lld", N1, N2, &tag, &radix);
if(tag == ){
strcpy(temp, N1);
strcpy(N1, N2);
strcpy(N2, temp);
}
Na = str2num(N1, radix);
int max = findMax(N2);
re = binSearch(max + , INT_MAX, N2, Na);
if(str2num(N2, re) != Na)
printf("Impossible");
else printf("%lld", re);
cin >> tag;
return ;
}
总结:
1、题意:给出a进制的N1, 未知进制的N2, 求出这个未知进制,使得a进制的N1 = 未知进制的N2。由于未知进制可能很大,故从可行的最小进制开始递增搜索不可行,会超时。只能用二分法。
2、当N2位数 >1时,只有一个解。但当N2是1位数时,会有多解。而题目要求输出最小的解,所以二分搜索可以采取寻找第一个使得N2 >= N1的进制。如果它使得N2 = N1,则寻找成功,如果它使得N2 > N1,则输出Impossible。
3、搜索上界设置为INT_MAX,要注意很大的radix在转换数字时会溢出,所以在mid 转换出的N2与 x 比较时要加上N2 > 0的条件。搜索下界应设置为使N1合法的最小进制,比如N1 = 1234,则下界置为5。
4、在str2num函数的循环的每一步中都可能出现溢出现象,一旦溢出要直接返回-1,否则有可能后续的转换又会使之变成正数。
5、int的最大值可以用INT_MAX,需要include<limits.h>。
6、需要打出一串字符串的结果最好直接复制样例,Impossible打错一个字母硬是有测试点过不去,研究了半天才发现。
A1010. Radix的更多相关文章
- PAT A1010.Radix 二分法
PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...
- PAT A1010 Radix (25 分)——进制转换,二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- A1010 Radix (25 分)
一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...
- PAT甲级——A1010 Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PTA A1009&A1010
第五天 A1009 Product of Polynomials (25 分) 题目内容 This time, you are supposed to find A×B where A and B a ...
- 1010 Radix (25 分)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 1 ...
- Java字节数组转按radix进制输出
代码如下: public class Main_bytesToStr { public static void main(String[] args) throws IOException { // ...
- 1010. Radix (25)(未完成)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- Trie / Radix Tree / Suffix Tree
Trie (字典树) "A", "to", "tea", "ted", "ten", "i ...
随机推荐
- zabbix邮件报警功能的验证
zabbix里面设置了很多监控项,有很多重要的监控预警,必须保证zabbix邮件报警功能正常,以确保那些告警信息能及时发送到运维人员的邮箱里. 所以需要每天8:30发一封确认zabbix邮件报警功能正 ...
- hash函数补分博客
题目要求: 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学 ...
- 运行scrapy crawl (文件名)时显示invalid syntax和no modle 'win32api'解决方案
使用pycharm爬取知乎网站的时候,在terminal端输入scarpy crawl zhihu,提示语法错误,如下: 原因是python3.7中将async设为关键字,根据错误提示,找到manho ...
- iOS Runloop理解
一.RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心 ...
- ABP框架用Dapper实现通过SQL访问数据库
ABP的框架(2) - 访问数据库 为了防止不提供原网址的转载,特在这里加上原文链接:http://www.cnblogs.com/skabyy/p/7517397.html 本篇我们实现数据库的 ...
- 现代程序设计 homework-02
首先显示博客要求: 描述在这么多相似的需求面前, 你怎么维护你的设计 (父类/子类/基类, UML, 设计模式, 或者其它方法) 让整个程序的架构不至于崩溃的? 建议从后往前来搞,比如我通读一遍需求 ...
- 面象对象设计原则之四:接口隔离原则(The Interface Segregation Principle,ISP)
接口隔离原则定义如下: 接口隔离原则(Interface Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 根 ...
- Docker(十六)-Docker的daemon.json的作用
docker安装后默认没有daemon.json这个配置文件,需要进行手动创建.配置文件的默认路径:/etc/docker/daemon.json 一般情况,配置文件 daemon.json中配置的项 ...
- [书摘]图解HTTP 状态码
状态码类别: 1XX informational 信息性状态码 2XX Suess 成功状态码 3XX Redirection 重定向状态码 4XX Client error 客户端错误状态码 5 ...
- linux_压缩解压命令(zip/tar)
一.zip 1.压缩 格式 $ zip (选项) (文件/目录) 选项 -r 参数表示递归打包包含子目录的全部内容. -q 参数表示为安静模式,即不向屏幕输出信息. -o 表示输出文件,需在其后紧跟打 ...