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[11]; char B[11]; long long least; long long num2Dec(char * p,long long radix) { long long len=strlen(p); long long m = 1; long long num = 1; long long sum = 0; for(long long i=len-1;i>=0;i--) { if(p[i]>='a'&&p[i]<='z') num= p[i] - 'a' + 10; else if(p[i]>='0'&& p[i]<='9') num=p[i] - '0'; sum+=num*m; m*=radix; } return sum; } long long findLowRadix(char *p) { long long len=strlen(p); long long low=0; long long num; for(long long i=len-1;i>=0;i--) { if(p[i]>='a'&&p[i]<='z') num= p[i] - 'a' + 10; else if(p[i]>='0'&& p[i]<='9') num=p[i] - '0'; if(num+1>low) low=num+1; } return low; } int compare(char* p,long long radix ,long long target) { long long len=strlen(p); long long m = 1; long long num = 1; long long sum = 0; for(long long i=len-1;i>=0;i--) { if(p[i]>='a'&&p[i]<='z') num= p[i] - 'a' + 10; else if(p[i]>='0'&& p[i]<='9') num=p[i] - '0'; sum+=num*m; m*=radix; if(sum>target) //avoid overflow return 1; } if(sum>target) return 1; else if(sum<target) return -1; else return 0; } 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>0) { high = mid-1; } else if(tmp<0) { low = mid +1; } else return mid; mid = (low + high)/2; } return -1; } 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(1==tag) { target=num2Dec(A,radix); least = findLowRadix(B); most = (target + 1 > least + 1) ? target +1 :least +1; res = binarySearch(B,least,most,target); if(res==-1) cout<<"Impossible"<<endl; else cout<<res<<endl; } else if(2==tag) { target=num2Dec(B,radix); least = findLowRadix(A); most = (target + 1 > least + 1) ? target +1 :least +1; res = binarySearch(A,least,most,target); if(res==-1) cout<<"Impossible"<<endl; else cout<<res<<endl; } }