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 ...
随机推荐
- github中non-fast-forward错误的解决
参考文献 1.http://www.linuxidc.com/Linux/2012-04/58985.htm 2.http://ihower.tw/blog/archives/2620 3.http: ...
- 【转】JavaScript中的this关键字使用的四种调用模式
http://blog.csdn.net/itpinpai/article/details/51004266 this关键字本意:这个.这里的意思.在JavaScript中是指每一个方法或函数都会有一 ...
- uiwebview的基本使用
http://blog.csdn.net/daiyelang/article/details/40989389
- CLH锁 、MCS锁
一.引文 1.1 SMP(Symmetric Multi-Processor) 对称多处理器结构,指服务器中多个CPU对称工作,每个CPU访问内存地址所需时间相同.其主要特征是共享,包含对CPU,内存 ...
- Erlang ERTS的Trap机制的设计及其用途
出处:http://mryufeng.iteye.com/blog/334744 erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到.它的实现需要erl vm的配合.它的 ...
- Java进程间通信
传统的进程间通信的方式有大致如下几种: (1) 管道(PIPE) (2) 命名管道(FIFO) (3) 信号量(Semphore) (4) 消息队列(MessageQueue) (5) ...
- JMX初体验
这些天在看<How Tomcat Works>这本书.里面讲到了JMX的内容.对我来说是个新知识点. JMX--Java Management Extensions,即Java管理扩展,是 ...
- Qt之窗体拖拽、自适应分辨率、自适应大小 good
Qt之自定义界面(实现无边框.可移动) Qt之自定义界面(窗体缩放-跨平台终极版) Qt之自定义界面(窗体缩放) http://blog.csdn.net/liang19890820/article/ ...
- zepto源码--qsa--学习笔记
zepto内部选择器qsa方法的实现. 简述实现原理: 通过判断传入的参数类型: 如果是'#id',则使用getElementById(id)来获取元素,并且将结果包装成数组形式: 如果是'.clas ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...