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 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample Output 2:
Impossible
题目本身不难,但是被绕晕了,参照柳姑娘了的代码。她里面用了二分查找,我偷懒试了一下结果 发现循环也不超时……
这题倒地考什么实在是不清楚
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
long long Transerves(string s1,long long radix)
{
long long sum =0;
int index=0,temp=0;
for(int i = s1.length()-1 ; i>=0;i--)
{
temp=s1[i]>='a'?s1[i]-'a'+10:s1[i]-'0';
sum+=temp*pow(radix,index++);
}
return sum;
}
long long Judge(string s1,long long num)
{
char it = *max_element(s1.begin(),s1.end());
long long low = (it>='a'?it-'a'+10:it-'0')+1;
long long high = max(num,low);
for(long long i = low ;i<=high;i++)
if(Transerves(s1,i)==num)return i;
/*while(low<=high)
{
long long mid = (low+high)/2;
long long t =Transerves(s1,mid);
if(t<0||t>num)high = mid-1;
else if(t==num) return mid;
else low = mid + 1;
}*/
return -1;
}
int main()
{
string s1,s2;
long long tag,radix,result_Radix;
cin>>s1>>s2>>tag>>radix;
result_Radix = tag==1?Judge(s2,Transerves(s1,radix)):Judge(s1,Transerves(s2,radix));
if(result_Radix!=-1)
printf("%lld",result_Radix);
else
printf("Impossible");
return 0;
}