PAT 解题报告 1010. Radix (25)
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)的更多相关文章
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- 【PAT甲级】1010 Radix (25 分)(二分)
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
- 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 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- 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 ...
随机推荐
- win7系统安装FAQ
安装失败: 直接重启后进入原系统 蓝屏 提示失败 开机F1进入BIOS-〉config-〉Serial ATA-〉将硬盘模式从AHCI改为Compatibility.这项很重要的.可以解决以上问题:
- JiaThis WordPress分享插件安装全攻略
WordPress 是一个功能非常强大的博客系统,插件众多,易于扩充功能.安装和使用都非常方便.目前 WordPress 已经成为主流的 Blog 搭建平台.按照下面的方法,只需简单几步,就可轻松地将 ...
- 固定某一元素在某一位置 jquery
方法: $(window).scroll(function (){}) 属性: 滚动变化值 $(window).scrollTop();$(window).scrollLeft(); 需要结合的样式: ...
- 如何删除docker images/containers
docker images往往不知不觉就占满了硬盘空间,为了清理冗余的image,可采用以下方法: 1.进入root权限 sudo su 2.停止所有的container,这样才能够删除其中的imag ...
- Machine Learning in Action -- 回归
机器学习问题分为分类和回归问题 回归问题,就是预测连续型数值,而不像分类问题,是预测离散的类别 至于这类问题为何称为回归regression,应该就是约定俗成,你也解释不通 比如为何logistic ...
- 智能手机,医疗诊断,云会议(gotomeeting/citrix)
在诊断领域已出现很多大有希望的创新,它们可能会起到真正的变革作用. 例如,有一种新技术可以让健康护理工作者用一部智能手机拍摄高质量的视网膜图像.这些数码照片像素很高,足以帮助检测白内障.黄斑退化.糖尿 ...
- 转:VS2010调试NUnit测试项目 (Running or debugging NUnit tests from Visual Studio without any extensions)
If you write unit tests and use NUnit test framework this may be helpful. I decided to write this si ...
- Qt high DPI
http://doc.qt.io/qt-5/highdpi.html Qt Support Ability to provide pixmaps or artwork for high resolut ...
- 设计模式:适配器模式(Adapter)
定 义:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 有些国家用110V电压,而我们国家用的是220V,但是我们的电器,比 ...
- Android性能优化典范(转)
转载自oschina. 2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍 ...