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:
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
题目大意:给出两个数(实际上是用字符串给出的),如果给出的字符是a~z,则对应10–35。给出一个数的进制,问另一个数有不有可能等于这个数,所求的数由tag决定。如果有,输出它应该为什么进制;否则输出Impossible。
PS:
1.所需要求的进制并没有给出范围,也就是说可以无限大。那么所转换出来的数就得用long long了。
2.如果范围很大,一个一个试会超时,要用到二分。
3.如果不用函数,代码写起来会觉得比较繁琐。如max,isdigit,isalpha等等。
下面给出我第一次提交的代码:
/*---------------------------------------------------------------------
第一次做想的很简单:
把已知进制的数转换为10进制,然后for循环去求另一个进制。
如果所求数已经大于了已知数,说明不可能相等,直接退出;如果相等,即为所求。
只有21分,结果我就不贴了...
上网查了之后才发现这题并不简单...
1.没有考虑到会爆long long
2.超时没有想到二分
----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
int convert(char *s, int radix)
{
int sum = 0;
int base = 1;
int i;
for(i = strlen(s)-1; i >= 0; i--)
{
int x;
if(s[i]>='a' && s[i]<='z')
x = s[i]-'a'+10;
else x = s[i]-'0';
sum += x*base;
base *= radix;
}
return sum;
}
int main()
{
char s[3][15];
int tag, radix;
scanf("%s%s%d%d", s[1], s[2], &tag, &radix);
int sum1, sum2, ans, i;
sum1 = convert(s[tag], radix);
for(ans = 2; ; ans++)
{
sum2 = convert(s[3-tag], ans);
if(sum2 >= sum1)
{
if(sum2 == sum1) printf("%d\n", ans);
else printf("Impossible\n");
break;
}
}
return 0;
}
在发现了要用二分之后,上下限又是一个问题:
- 对于下限:被求的那个数各位数字的最大值加+1。比如367,那它肯定至少是8进制的数。
- 对于上限:应该是已知进制的那个数所转换出来的十进制数。但是有特殊情况:
比如N1=14 ,N2=1z,tag=1,radix=2。这时由于N2至少是一个35+1=36进制的数,所以不能取N1的十进制数,而应取二者的最大值。
下面给出AC代码:
#include <iostream>
#include <string>
#include <cctype>//有isdigit(),isalpha()函数
#define ll long long//要用到longlong
using namespace std;
ll convert(string s, int radix)
{
int i;
ll sum = 0;
for(i=0; i < s.size(); i++)
{
sum = sum*radix;
if(isdigit(s[i])) sum += s[i]-'0';
else sum += s[i]-'a'+10;
}
return sum;
}
ll solve(ll sum1, string s2)
{
ll l = -1, r = -1;
int i;
for(i = 0; i < s2.size(); i++)
if(isdigit(s2[i]))
if(l < s2[i]-'0') l = s2[i]-'0';
else
if(l < s2[i]-'a'+10) l = s2[i]-'a'+10;
++l;//找出所求串的最大字符,然后+1
r = max( l, sum1);//取二者最大值
while(l <= r)//二分
{
ll mid = (l+r)/2;
ll sum = convert( s2, mid);
if(sum>sum1 || sum<0) r = mid-1;/*由于所求值可能过大,连longlong都装不下,那么就会变成负数。所以
如果sum<0的话,也表明进制过大。*/
else if(sum == sum1) return mid;
else l = mid+1;
}
return -1;
}
int main()
{
string s1, s2;
int tag, radix;
cin >> s1 >> s2 >> tag >> radix;
if(tag == 2) swap(s1, s2);//这样可以保证s1是已知进制的,后续处理就方便一点
ll sum1, sum2;//两个串所求出的十进制数
sum1 = convert(s1, radix);
ll ans = solve(sum1, s2);
if(ans == -1) cout << "Impossible" << endl;
else cout << ans << endl;
return 0;
}
总结:
- 与数学有关的题目,要注意是否要用long long。
- 要熟悉一些常用的函数,减小代码量。另外要掌握一点STL的知识,写起来也会简单很多。
- 如果题目给出要处理的数据如上述题目根据tag而定的话,可以交换二者,保证后者是要处理的,方便后续处理。
- 如果一个非十进制数用字符串表示的,将它转换为十进制有两种写法:
/*
s是串
sum是待求十进制数
radix是当前进制
*/
sum = 0;
for(i = 0; i < s.size(); i++)
{
sum = sum*radix+s[i]-'0';
}
/* or */
base = 1;
for(i = s.size()-1; i >= 0; i--)
{
sum += (s[i]-'0')*base;
base *= radix;
}