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. WPF 录屏软件研发心得及思路分享(已结束开发)

    最近由于工程需要开始研发基于Windows的自动录屏软件,很多细节很多功能需要处理,毕竟一个完美的录屏软件不是你随随便便就可以写出来的.首先参考了大部分的录屏软件,在研发的过程中遇到了很多的问题:比如 ...

  2. JS_各种排序方法

    排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 我们这里说说八大排序就是内部排序. 当n较大,则应采 ...

  3. sheet制作返回按钮

    =HYPERLINK("#目录!A1","目录!A1") =HYPERLINK("#"&A2&"!A1" ...

  4. alpa开发阶段团队贡献分

    这是我们团队之前决定的分配方式: 1.凡是认真完成自己任务的队员,都将有基础分30分(态度分). 2. 将整个项目细化为不同的任务,列出一个任务清单,在综合.协调完每名成员的意愿后,我会分配清单中的任 ...

  5. Linux内核分析(第八周)

    进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与其时机分析 分类: 第一种分类 I/O-bound:频繁的进行I/O:会花很多时间等待I/O操作完成 CPU ...

  6. 使用Junit进行单元测试

    使用Junit进行单元测试 一.目的和要求 JUnit是一款由Erich Gamma(<设计模式>的作者)和Kent Beck(极限编程的提出者)编写的开源的回归测试框架,供Java编码人 ...

  7. python 生成器、列表解析式、yield、迭代器

    开局一张图总结关系 一.列表解析式 我们习惯生成列表通过list = [1, 2, 3]的方式.还有一种很方便的列表生成方式 list = [a*2 for a in range(10)],或者lis ...

  8. tensorflow在windows下的安装

    1.python 的安装 这里我选择的是Anaconda4.2,附上下载链接https://www.continuum.io/downloads 2.测试python安装是否成功 在cmd中输入pyt ...

  9. MYSQL使用中字符编码一坑

    AJAX提交的字符出错,还以为是AJAX配置的错误呢!幸亏检查了一下MYSQL连接的字符集,发现开发库与本地库配置的字符集是也不一样的.

  10. SQLserver 一种简单的GUI方式创建DBlink copy 表数据的方法

    1. 在sqlserver 上面使用GUI的方式创建dblink 首先打开查询分析器 在如下的位置处右键 -新建连接服务器 输入需要copy数据的服务器 输入ip地址 然后建立连接 在打开查询分析器进 ...