1010. Radix (25)

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

题意

给定两个数,其中单个位置上的数值范围可以为 [0-z]。指定其中一个数的进制,试确定是否存在可能的进制让两数的实际值相等。

分析

此题没有交代清楚 input 中 radix 的取值范围以及对一位数有多重可能 radix 的情况如何输出,坑比较大。下面是需要注意的点。

  • 1.input 中两个数字可以是 10 位数,虽然没有告诉 radix 的范围,但在9*10^10 10 1 200这个示例中,可以看到结果的 radix 也可以是很大的。从这个角度看,代码中将 radix 和两个数值都设定为 longlong 是合适的选择。
  • 2.在计算另一个数的 radix 时,简单的遍历 [2, 1018]会超时。单调的区间很自然想到使用二分查找。
  • 3.二分查找的上下界确定能减少耗时:下界选数字的所有位上的最大值+1;上界容易想当然的认为就是题中给定了 radix 的数的值。实际上,示例11 b 1 10就是一个反例,原因在于这个假设忽略了一位数的可能性,解决方案是在取给定 radix 的数值和下界中较大的那个数。
  • 4.在二分查找时,不可直接计算出某个 radix 下数的值,因为可能会 longlong 溢出。于是需要用特定的 compare 函数,在累加的过程中判定是否大于另一个数。算是一种剪枝。
  • 5.还有一个条件:当两个数都是 1 时,输出 2.当两个数相等且不为 1 时,输出题中给出的 radix。
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cstring> using namespace std;
//AC代码
char A[];
char B[];
long long least; long long num2Dec(char * p,long long radix)
{
long long len=strlen(p);
long long m = ;
long long num = ;
long long sum = ;
for(long long i=len-;i>=;i--)
{
if(p[i]>='a'&&p[i]<='z')
num= p[i] - 'a' + ;
else if(p[i]>=''&& p[i]<='')
num=p[i] - '';
sum+=num*m;
m*=radix;
}
return sum;
} long long findLowRadix(char *p)
{
long long len=strlen(p);
long long low=;
long long num;
for(long long i=len-;i>=;i--)
{
if(p[i]>='a'&&p[i]<='z')
num= p[i] - 'a' + ;
else if(p[i]>=''&& p[i]<='')
num=p[i] - '';
if(num+>low)
low=num+;
}
return low; } int compare(char* p,long long radix ,long long target)
{
long long len=strlen(p);
long long m = ;
long long num = ;
long long sum = ;
for(long long i=len-;i>=;i--)
{
if(p[i]>='a'&&p[i]<='z')
num= p[i] - 'a' + ;
else if(p[i]>=''&& p[i]<='')
num=p[i] - '';
sum+=num*m;
m*=radix;
if(sum>target) //avoid overflow
return ;
} if(sum>target)
return ;
else if(sum<target)
return -;
else
return ; } long long binarySearch(char *p,long long low,long long high,long long top)
{
long long mid = low;
long long tmp; while(low<=high)
{
tmp = compare(p,mid,top);
if(tmp>)
{
high = mid-;
}
else if(tmp<)
{
low = mid +;
}
else
return mid;
mid = (low + high)/;
} return -;
} int main()
{
long long tag;
long long radix;
long long target;
long long least; // lowest possible radix
long long most; // highest possible radix
long long res; cin>>A;
cin>>B;
cin>>tag;
cin>>radix; if(==tag)
{
target=num2Dec(A,radix);
least = findLowRadix(B);
most = (target + > least + ) ? target + :least +;
res = binarySearch(B,least,most,target);
if(res==-)
cout<<"Impossible"<<endl;
else
cout<<res<<endl; }
else if(==tag)
{
target=num2Dec(B,radix);
least = findLowRadix(A);
most = (target + > least + ) ? target + :least +;
res = binarySearch(A,least,most,target);
if(res==-)
cout<<"Impossible"<<endl;
else
cout<<res<<endl;
} }

PAT 解题报告 1010. Radix (25)的更多相关文章

  1. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  2. PAT (Advanced Level) 1010. Radix (25)

    撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...

  3. 【PAT甲级】1010 Radix (25 分)(二分)

    题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...

  4. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

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

  5. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  6. 已经菜到不行了 PAT 1010. Radix (25)

    https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...

  7. PAT Advanced 1010 Radix(25) [⼆分法]

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

  8. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  9. PAT 解题报告 1051. Pop Sequence (25)

    1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...

随机推荐

  1. 纸牌project

    用range[0,8)的列表表示牌,这些数字要出现两次.我们建议你通过连接两个range[0,8)的列表来创建这个list.利用Docs来安排列表串联操作 写一个draw handler啥样的draw ...

  2. 在HCI层ACL Connection的建立

    一.概述     上一篇博文介绍的是inquiry的整个过程中HCI层的command和event.在寻找到有效的远端蓝牙设备后,开始建立ACL连接,这里仅仅反应HCI层的数据包,对于LM层和Base ...

  3. 巧用AWK处理二进制数据文件

    AWK是Unix下的一款功能强大的文本格式化和抽取工具.利用这个工具,可以对复杂的文本文件进行整理,提取其中的全部或者部分数据,按照需要的格式予以显示.需要说明的是,AWK的强大功能只针对纯文本文件. ...

  4. 读书笔记——《图解TCP/IP》(1/4)

    读书笔记——<图解TCP/IP>(1/4) 经典摘抄 第一章 网络基础知识 1.独立模式:计算机未连接到网络,各自独立使用的方式. 2.广域网 WAN 局域网 LAN 城域网 MAN 3. ...

  5. sphinx

    1.什么是SphinxSphinx 是一个在GPLv2 下发布的一个全文检索引擎,商业授权(例如, 嵌入到其他程序中)需要联系我们(Sphinxsearch.com)以获得商业授权.一般而言,Sphi ...

  6. 修改delphi xe6 FMX Label字体颜色

    delphi fmx的字体等设置默认与皮肤有关,用代码直接修改字体颜色等是无效的,如何才能用代码修改呢?请按以下方法就可以: 1.在Object inspector中取消StlyedSettings中 ...

  7. Bulk Insert & BCP执行效率对比

    我们以BCP导出的CSV数据文件,分别使用Bulk insert与BCP导入数据库,对比两种方法执行效率 备注:导入目标表创建了分区聚集索引 1.BCP导出csv数据文件 数据量:15000000行, ...

  8. php之curl实现http与https请求的方法

    原文地址:http://m.jb51.net/show/56492   这篇文章主要介绍了php之curl实现http与https请求的方法,分别讲述了PHP访问http网页与访问https网页的实例 ...

  9. SQLSERVER 16进制转10进制

    原码.补码.反码参考: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html 进制转换参考: http://ww ...

  10. JMeter学习-014-JMeter 配置元件实例之 - 用户定义的变量 参数化配置

    前文讲述了通过 CSV Data Set Config 实现参数化配置(详情敬请参阅:JMeter学习-010-JMeter 配置元件实例之 - CSV Data Set Config 参数化配置), ...