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 ...
随机推荐
- GNU构建系统和AutoTools
注:本篇博客是阅读文末[参考博客]的讲解所写,内容非原创,仅是学习笔记 1. 概述2. 不同视角的程序构建2.1 用户视角2.2 开发者视角3. 导图图片4. configure选项参考博客 1. 概 ...
- Object-Oriented(二)原型对象
自用备忘笔记 1. 理解原型对象 只要创建函数,函数上就会创建一个 prototype 属性指向函数的原型对象. function Person() {} Person.prototype //指向该 ...
- Python - 内置函数 选例
概览参见 https://www.runoob.com/python/python-built-in-functions.html 官方文档 https://docs.python.org/3/li ...
- python之requests
发送请求 导入 Requests 模块: >>> import requests >>> r = requests.get('https://xxxxxxx.jso ...
- 团队作业Week14——源代码管理
0. 在吹牛之前,先回答这个问题: 如果你的团队来了一个新队员,有一台全新的机器, 你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新.最稳定版本的软件编译出 ...
- Scrum Meeting day 3
第三次会议 No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 No_11:照片记录 No_100:代码/文档签入记录
- David Silver强化学习Lecture3:动态规划
课件:Lecture 3: Planning by Dynamic Programming 视频:David Silver强化学习第3课 - 动态规划(中文字幕) 动态规划 动态(Dynamic): ...
- 好文章系列C/C++——图说C++对象模型:对象内存布局详解
注:收藏好文章,得出自己的笔记,以查漏补缺! ------>原文链接:http://blog.jobbole.com/101583/ 前言 本文可加深对C++对象的内存布局.虚表指针.虚 ...
- python格式化字符串Type Error: Format Requires Mapping 的问题
最近几天 频繁看到有这种写法 BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s" 第一次看到的pythoner看到可能会有点懵逼 ...
- python的数据相关框架
ipython 多种编程语言之间进行交互计算的命令行shell graphlab greate 快速构建大型高性能数据产品 pandas 数据分析 pulp 线性编程模型 matplotlib sci ...