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. php常用[字符串]函数

    nl2br 功能:化换行符为<br> <?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $ ...

  2. bootstrap static popover

    jq $('.popover').show().css('position','relative');

  3. a compromise between lock overhead and data safety

    High Performance My SQL  THIRD EDITION A locking strategy is a compromise between lock overhead and ...

  4. 2认识HTML中的“ML”:深入理解超文本

    HTML是描述网页结构的标记语言(即HTML中的'ML'),而HT指把一个网页链接到其他网页. <a>元素可以创建超文本链接到另外一个网页,<a>元素中的内容在网页中是可点击的 ...

  5. Intel Visual Fortran Compiler 11调用lapack库实现并行多处理计算

    采用fortran进行数值计算的朋友们都应该听说过大名鼎鼎的lapack库,我就不多做介绍了,在此,我只是介绍一个编译好的lapack二进制包ACML(AMD Core Math Library),并 ...

  6. ROSE User Case View

    用例视图(User Case View) 在面向对象的分析过程中,此视图应该是需求分析的过程中采用,主要包括如下过程 01涉众分析--->02业务分析--->03概念分析--->04 ...

  7. MvcPager分页控件的使用

    1.引入MvcPager.dll(MvcPager分页控件:http://www.webdiyer.com/mvcpager/) 2.后台C# Controller: //Ddemo使用Webdiye ...

  8. Bluetooth ATT介绍

    目录 1 介绍 2 详细内容 2.1 Attribute Type 2.2 Attribute Handle 2.3 Attribute Handle Grouping 2.4 Attribute V ...

  9. Mac OS X 上启动 FTP/SFTP server,并设置 log level

    木易小伟的博客| 木易小伟的博客 2013-08-13 5708 阅读 FTP Log SFTP Mac OS 系统配置 1. 启动FTP Server: 命令行下,  sudo -s launch ...

  10. easy UI简单使用介绍

    http://www.cnblogs.com/yokoboy/archive/2012/12/06/2806132.html