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的更多相关文章

  1. PAT A1010.Radix 二分法

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

  2. PAT A1010 Radix (25 分)——进制转换,二分法

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

  3. A1010 Radix (25 分)

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

  4. PAT甲级——A1010 Radix

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

  5. PTA A1009&A1010

    第五天 A1009 Product of Polynomials (25 分) 题目内容 This time, you are supposed to find A×B where A and B a ...

  6. 1010 Radix (25 分)

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

  7. Java字节数组转按radix进制输出

    代码如下: public class Main_bytesToStr { public static void main(String[] args) throws IOException { // ...

  8. 1010. Radix (25)(未完成)

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

  9. Trie / Radix Tree / Suffix Tree

    Trie (字典树) "A", "to", "tea", "ted", "ten", "i ...

随机推荐

  1. pyenv+virtual 笔记

    Pyenv + virtualEnv 设置 安装这两个组件是为了适应不同版本的python在同一个系统下的运行:例如现在最明显就是python2.7和python3.6的两个版本,很多库依旧是使用了P ...

  2. Java各厂对外的优质博客

    1.美团:https://tech.meituan.com/ 2.极客学院:http://wiki.jikexueyuan.com/list/java/

  3. Redis主从复制原理总结

    和Mysql主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或者级联结构,Redi ...

  4. 《Linux内核分析》 第八节 进程的切换和一般的执行过程

    张嘉琪 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核分析 第八 ...

  5. 2017-2018-2 1723《程序设计与数据结构》实验四 & 实验五 & 课程总结 总结

    作业地址 实验四作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1943 提交情况如图: 实验五作业:https://edu ...

  6. Android 學習之旅!(1)

    就這樣就過去了一年加一個學期,現在是大二第二個學期而且是下半學期了,以前都是無所事事,沒事睡睡覺,打打遊戲就過去了,但是想到家境和以後的路,我還是決心自己找點東西學習下,以後出去還能有一技之長(雖然可 ...

  7. ABP框架用Dapper实现通过SQL访问数据库

    ABP的框架(2) - 访问数据库   为了防止不提供原网址的转载,特在这里加上原文链接:http://www.cnblogs.com/skabyy/p/7517397.html 本篇我们实现数据库的 ...

  8. PAT 1019 数字黑洞

    https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968 给定任一个各位数字不完全相同的4位正整数,如 ...

  9. SyntaxHighlighter行号显示错误问题解决方案

    SyntaxHighlighter是根据代码中的换行符分配行号的.但是,如果一行代码或者注释比较长,在页面显示时需要分成多行显示,会出现行号对不上的问题,像这样: 通过设置CSS强制不换行,可以保证行 ...

  10. 使用docker-compose 大杀器来部署服务

    使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker,然而使用方式却是仅仅用手动的方式,这样去操作 docker 还是很原始. 好吧,可能在小白的眼中噼里啪啦的对着 ...