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 ...
随机推荐
- Ubuntu+Nginx+PHP的最简搭建方法
先安装: sudo apt-get install nginx php5-fpm -y 然后编辑配置文件: /etc/nginx/site-available/default 找到"loca ...
- WEB系统开发
最近根据项目组需求做了web工作流程系统,就是一个简单的web管理系统,取代原先使用的excel表格,在VS2008下面开发,数据库使用Mysql,使用Mysql-connecte 5.0与数据库连接 ...
- Jquery--array
--遍历数组 $.each(Array, function(i, value) { this; //this指向当前元素 i; //i表示Array当前下标 value; //value表示Array ...
- lvs简单配置
负载均衡服务器将会用到两块网卡,eth0为公网地址(此处将局域网ip作为公网地址),IP地址为192.168.1.104,eth0:1,IP地址为192.168.2.1在负载均衡器上添加一个ip别名, ...
- jquery autocomplete插件结合ajax使用
<%@ page isELIgnored="false"%> <%@ page contentType="text/html; charset=UTF- ...
- 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...
- VS小技巧
1."清理解决方案":在对程序进行分发.上传时时常需要压缩解决方案文件夹,这时如果还嫌文件太大,可以在VS里右键解决方案---清理解决方.完成后,则该解决方案下的所有项目的将所有中 ...
- 通过全局设置过滤器,就能让所有窗口都可移动,而不是都要继承指定QDialog
#include "appinit.h" #include <QMouseEvent> #include <QApplication> #include & ...
- 让android webView使用系统默认浏览器内核直接解析,不弹出选择浏览器选项
遇到一个需求,要求浏览网页的页面不去启动其他的浏览器,全部在自身的应用中. 解决方法 webview.setWebViewClient(new WebViewClient() { @Override ...
- JS之相等操作符
1.相等 == 和不相等 != 先转换操作数再比较相等性 在转换不同的数据类型时,相等和不相等遵循以下规则: 1.1 如果有一个操作数是布尔值,则在比较相等性前先将其转换为数值---false转换为0 ...