Radix
题目
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
答案
不用二分法(测试点7无法通过)
#include<iostream>
using namespace std;
int main()
{
int flag,radix;
string num1,num2,tmp;
cin>>num1>>num2>>flag>>radix;
int sum=0;
if(flag==1)
{
tmp=num2;
for(int i=0;num1[i];i++)
{
if(num1[i]>='0'&&num1[i]<='9') sum=sum*radix+num1[i]-'0';
else sum=sum*radix+num1[i]-'a'+10;
}
}
else
{
tmp=num1;
for(int i=0;num2[i];i++)
{
if(num2[i]>='0'&&num2[i]<='9') sum=sum*radix+num2[i]-'0';
else sum=sum*radix+num2[i]-'a'+10;
}
}
int flg=0;
for(int i=1;i<=50;i++)
{
string temp=tmp;
int sum1=0,j;
for(j=0;temp[j];j++)
{
if(temp[j]>='0'&&temp[j]<='9')
{
if(temp[j]-'0'>=i) break;
sum1=sum1*i+temp[j]-'0';
}
else
{
if(temp[j]-'a'+10>=i) break;
sum1=sum1*i+temp[j]-'a'+10;
}
}
if(temp[j]) continue;
if(sum1==sum)
{
cout<<i<<endl;
return 0;
}
}
cout<<"Impossible";
}
使用二分法(完美通过)
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
long long get_sum(string s,long long radix)
{
long long sum=0;
for(int i=0;s[i];i++)
{
long long t=isalpha(s[i])?(s[i]-'a'+10):(s[i]-'0');
sum=sum*radix+t;
}
return sum;
}
int main()
{
string s1,s2,tmp;
long long flag,radix,sum,tmp_sum;
cin>>s1>>s2>>flag>>radix;
if(flag==1)
{
tmp=s2;
sum=get_sum(s1,radix);
}
else
{
tmp=s1;
sum=get_sum(s2,radix);
}
char ch=*max_element(tmp.begin(),tmp.end());
long long low=isalpha(ch)?(ch-'a'+10+1):(ch-'0'+1);
long long high=max(low,sum);
while(low<=high)
{
long long mid=(low+high)/2;
tmp_sum=get_sum(tmp,mid);
if(tmp_sum<0||tmp_sum>sum) high=mid-1;
else if(tmp_sum<sum) low=mid+1;
else
{
cout<<mid;
return 0;
}
}
cout<<"Impossible";
}
参考
总结
引子
在具体分析前,我要先说明一下,这道题不用二分法做也能拿到24分,但如果想要追求完美且为了能在大赛中取得更好的成绩,我个人建议大家还是要会二分法的写法
这道题二分法的相关部分反倒问题不大,重要的是很多细节的处理,我从头为大家梳理一下
一、变量类型
整段代码,除了字符串变量,都要设为long long变量,因为题目给的数可能会非常大
二、求值函数
get_sum函数是求对应进制的数值,我这里采用的方法和参考文章并不相同,我是从头开始的计算。
同时需要注意的是,一定要先将要加的数转为long long变量再相加,即:
long long t=isalpha(s[i])?(s[i]-'a'+10):(s[i]-'0');
sum=sum*radix+t;
而非直接相加,因为isalpha(s[i])?(s[i]-'a'+10):(s[i]-'0')
返回的是int类型,它与long long类型的sum相加肯定会出错
三、max_element函数
max_element函数是algorithm库中非常好用的函数,它用以返回数组的最大值
举例:max_element(r, r+6),返回数组r中[0, 6)之间的最大值的迭代器,加上*就是对应的元素,而使用max_element返回的值减去数组头地址即为该最大值在数组的序号
但如果想要获取字符串中的最大值时,就要用代码中的这种方式
char ch=*max_element(tmp.begin(),tmp.end());
这点和对vector对象进行sort排序时很像
sort(vec.begin(),vec.end())
四、进制的最大值和最小值(重点来了)
首先,很显然,进制的下限是目标数各位数字的最大数字加1
举例:12345,它的进制最小值为6,因为六进制最大数是5符合题意,五进制的最大数是4不合题意
进制的最大值就是我们已经求得的数字的值,拿题目的“6 110 1 10”举例,我们已经求得了十进制数6的值为6,那么110的进制的最大值也就是6,因为当目标数110的位数大于1时,它的最高位所对应的值即1*62=36一定大于等于6(最低位对应权值为60,次低位对应权值为61……以此类推。所以只要目标数超过1位,那么它的最高位一定不是0,乘上对应权值自然就比6大)
当然,如果目标数是1位,我们就取求得的值和最小值的较大者,以使后面的循环至少进行一次比较,即:
long long high=max(low,sum);
五、为什么sum有可能小于0
因为如果sum的值特别大,已经超越了long long的范围,就会变为负数,但实际上这个sum值是个非常大的数且一定大于sum,所以要将其放入大于sum的比较中,即:
if(tmp_sum<0||tmp_sum>sum) high=mid-1;