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. sigar开发(java)

    下载sigar,地址:https://yunpan.cn/cBEWbEfdAm98f (提取码:f765) 可以收集的信息 CPU信息:包括基本信息(vendor.model.mhz.cacheSiz ...

  2. php 中 opendir() readdir() scandir()

    opendir(path,context)若成功,则该函数返回一个目录流,否则返回 false 以及一个 error.可以通过在函数名前加上 “@” 来隐藏 error 的输出. readdir() ...

  3. python基础学习笔记(十三)

    re模块包含对 正则表达式.本章会对re模块主要特征和正则表达式进行介绍. 什么是正则表达式 正则表达式是可以匹配文本片段的模式.最简单的正则表达式就是普通字符串,可以匹配其自身.换包话说,正则表达式 ...

  4. Python进阶量化交易专栏场外篇7- 装饰器计算代码时间

    欢迎大家订阅<教你用 Python 进阶量化交易>专栏!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外已陆续推出一些手记来辅助同学们学习本专栏内容,目前已推出如下扩展篇: 在第一篇 ...

  5. 读后感for《一个程序员的生命周期》

    我是村里走出来的孩子,妈妈说我也许是家里唯一一个大学生了,家里从选专业开始也赋予我厚望.说实话,上大学是父母经济压力最大的时候.心疼,大概就是早上六七点起床,看到爸爸一夜没睡,带着倦容眼睛红红的还在工 ...

  6. 小学四则运算APP 第一阶段冲刺

    需求分析 1.相关系统分析员向用户初步了解需求,然后用word列出要开发的系统的大功能模块,每个大功能模块有哪些小功能模块,对于有些需求比较明确相关的界面时,在这一步里面可以初步定义好少量的界面.[1 ...

  7. 【软件工程】5.8 黑盒&白盒测试

    代码链接:http://www.cnblogs.com/bobbywei/p/4469145.html#3174062 搭档博客:http://www.cnblogs.com/Roc201306114 ...

  8. Resolved validation conflict with readonly

    /** * Bug绕过去方案WorkAround * Bug描述: * JQuery的Validation的和form的input元素设为readonly,一对不可调和的矛盾: * 一个设置为requ ...

  9. PP生产订单成本的计划、控制和结算

    SAP系统成本分析功能关注订单的成本,通过对计划成本和实际成本的比较分析,可以发现成本控制上的问题,以便及时解决问题.1.订单成本计划在基础数据齐全的基础上,系统可以自动滚算生产订单的成本.生产订单计 ...

  10. java List<String>的初始化

    今天在处理生成excel的时候用到了java的list,但是需要直接赋值固定的几个变量,如果先初始化然后add的方法: List<String> name = new ArrayList( ...