简直是哭着做出来的,起码提交了22遍。创纪录了。。。
首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了。。。
考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找。
注意,进制数不能小于数中最大的数字。
如果将两个数字作为字符串输入,注意排除字符串前面的0.
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
来源: <http://www.patest.cn/contests/pat-a-practise/1010>
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#pragma warning(disable:4996)
using namespace std;
long long int GetNumofStr(string s,int index) {
if (index < 2)
return -1;
long long int n = 0;
for (int i = 0; i < s.length(); i++) {
n *= index;
if (s[i] >= ‘0‘&&s[i] <= ‘9‘)
n += s[i] - ‘0‘;
else if (s[i] >= ‘a‘&&s[i] <= ‘z‘)
n += s[i] - ‘a‘ + 10;
}
return n;
}
int main(void) {
string s1, s2;
int tag, index;
cin >> s1 >> s2 >> tag >> index;
long long int s1num = 0, s2num = 0;
s1num = GetNumofStr(s1, 10);
s2num = GetNumofStr(s2, 10);
if (s1num == 0 && s2num == 0) {
cout << "1";
return 0;
}
else if (s1num*s2num == 0) {
cout << "Impossible";
return 0;
}
if (tag == 2) {
string temp;
temp = s1;
s1 = s2;
s2 = temp;
}
bool zeroHeadRemove = false;
string stemp;
for (int i = 0; i < s2.length(); i++) {
if (s2[i] != ‘0‘)
zeroHeadRemove = true;
if (zeroHeadRemove == true)
stemp += s2[i];
}
s2 = "";
s2 = stemp;
s1num = GetNumofStr(s1, index);
int startIndex = 0;
long long int temp;
if (s2[s2.length() - 1] >= ‘0‘&&s2[s2.length() - 1] <= ‘9‘) {
temp = s1num - s2[s2.length() - 1]+‘0‘;
}
else if (s2[s2.length() - 1] >= ‘a‘&&s2[s2.length() - 1] <= ‘z‘) {
temp = s1num -s2[s2.length() - 1]+‘a‘-10;
}
if (s2[0] >= ‘0‘&&s2[0] <= ‘9‘) {
temp /= (s2[0] - ‘0‘);
}
else if (s2[0] >= ‘a‘&&s2[0] <= ‘z‘) {
temp /= (s2[0] - ‘a‘+10);
}
startIndex = pow(temp, 1.0 / (s2.length() - 1));
char s2max = 0;
for (int i = 0; i < s2.length(); i++) {
if (s2[i] > s2max)
s2max = s2[i];
}
if (s2max >= ‘0‘&&s2max <= ‘9‘) {
if (startIndex < s2max-‘0‘)
startIndex = s2max - ‘0‘+1;
}
if (s2max >= ‘a‘&&s2max <= ‘z‘) {
if (startIndex < s2max - ‘a‘+10)
startIndex = s2max - ‘a‘+11;
}
bool possible = false;
while (true)
{
s2num = GetNumofStr(s2, startIndex);
if (s2num == s1num) {
possible = true;
break;
}
else if (s2num < s1num) {
break;
}
startIndex--;
if (s2max >= ‘0‘&&s2max <= ‘9‘) {
if (startIndex < s2max - ‘0‘)
break;
}
if (s2max >= ‘a‘&&s2max <= ‘z‘) {
if (startIndex < s2max - ‘a‘ + 10)
break;
}
}
if (possible == true) {
cout << startIndex;
}
else
cout << "Impossible";
return 0;
}