1010. Radix (25)——PAT (Advanced Level) Practise

题目信息:

1010. Radix (25)

时间限制

400 ms

内存限制

32000 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

代码如下:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

long long strtol(string &str, long long rdx)
{
	long long re = 0;
	long long n = 1;
	char ch;
	int t;
	for (int i = str.size() - 1; i >= 0; i--)
	{
		ch = str[i];
		if (ch <= '9')
			t = ch - '0';
		else
			t = ch - 'a' + 10;
		re += t * n;
		n *= rdx;
	}
	return re;
}
int cmp(string str, long long rdx, long long n1)
{
	long long sum = 0;
	long long n = 1;
	char ch;
	int t;
	for (int i = str.size() - 1; i >= 0; i--)
	{
		ch = str[i];
		if (ch <= '9')
			t = ch - '0';
		else
			t = ch - 'a' + 10;
		sum += t * n;
		n *= rdx;
		if (sum > n1)
			return 1;
	}
	if (sum < n1)
		return -1;
	else if (sum > n1)
		return 1;
	else
		return 0;
}
long long binarysearch(string str, long long min, long long max, long long n)
{
	long long mid = min;
	while (min <= max)
	{
		int i = cmp(str, mid, n);
		if (i == 0)
			return mid;
		else if (i == 1)
			max = mid - 1;
		else
			min = mid + 1;
		mid = (min + max) / 2;
	}
	return -1;
}
int main()
{
	string str1, str2;
	cin >> str1 >> str2;
	long long i, radix;
	cin >> i >> radix;
	if (i == 2)
	{
		str1.swap(str2);
	}
	long long n1 = strtol(str1, radix);

	if (n1 == 1 && str2 == "1") //这两条奇葩的条件!!
	{
		cout << "2" << endl;
		return 0;
	}
	else if (str1 == str2)
	{
		cout << radix << endl;
		return 0;
	}

	char crds = *max_element(str2.begin(), str2.end());
	int redx;
	if (crds <= '9')
		redx = crds - '0' + 1;
	else
		redx = crds - 'a' + 10 + 1;
	int dx = (redx > n1) ? redx : n1 ;
	i = binarysearch(str2, redx, dx, n1);
	if (i == -1)
		cout << "Impossible" << endl;
	else
		cout << i << endl;
	return 0;
}
时间: 2024-08-25 08:26:14

1010. Radix (25)——PAT (Advanced Level) Practise的相关文章

1016. Phone Bills (25)——PAT (Advanced Level) Practise

题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending

1007. Maximum Subsequence Sum (25)——PAT (Advanced Level) Practise

题目信息: 1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <=

1006. Sign In and Sign Out (25)——PAT (Advanced Level) Practise

题目信息: 1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock th

1009. Product of Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each c

1002. A+B for Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lin

1003. Emergency (25)——PAT (Advanced Level) Practise

题目信息: 1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads.

1012. The Best Rank (25)——PAT (Advanced Level) Practise

题目信息: 1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Math

1013. Battle Over Cities (25)——PAT (Advanced Level) Practise

题目信息: 1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward th

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c