PAT_A 1010 Radix

发布时间 2023-10-21 11:52:10作者: 永无荒城

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

总结

二分 刚开始二分查找写错,导致只能得17分,修改后ac。

// 二分写错
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tag,radix;
string a,b;
ll decimalNum(string a, ll r){
	ll t = 0, e = 0, c = 0;
	for(ll i = 0; i < a.size() ; i++){
		if('0' <= a[i] && a[i] <= '9'){
			e = a[i] - '0';
		}else{
			e = a[i] - 'a' + 10;
		}
		t = t*r + e;
		if(t < 0) return -1;
	}
	return t;
}
ll radixMin(string a){
	ll m = 2, e = 0;
	for(ll i = 0; i < a.size() ; i++){
		if('0' <= a[i] && a[i] <= '9'){
			e = a[i] - '0';
		}else{
			e = a[i] - 'a' + 10;
		}
		m = max(m, e+1);
	}
	return m;
}
int main(){
	cin>>a>>b>>tag>>radix;
	if(a==b){
		cout<<radix;
		return 0;
	}
	if(tag == 2) swap(a,b);
	ll c = decimalNum(a, radix);
	ll lo = max(radixMin(b), 2LL);
	ll hi = max(lo, c)+1+1;
	ll t = 0, mi = 0;
	while(lo < hi){
		mi = (lo+hi)/2;
		t = decimalNum(b, mi);
		if(t > 0 && t < c){
			lo = mi + 1;
		}else{
			hi = mi;
		}
	}
	t == c ? cout<<mi : cout<<"Impossible";
	return 0;
}
// 二分修改
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tag,radix;
string a,b;
ll decimalNum(string a, ll r){
    ll t = 0, e = 0, c = 0;
    for(ll i = 0; i < a.size() ; i++){
        '0' <= a[i] && a[i] <= '9' ? e = a[i] - '0' : e = a[i] - 'a' + 10;
        t = t*r + e;
        if(t < 0) return -1;
    }
    return t;
}
ll radixMin(string a){
    ll m = 2, e = 0;
    for(ll i = 0; i < a.size() ; i++){
        '0' <= a[i] && a[i] <= '9' ? e = a[i] - '0' : e = a[i] - 'a' + 10;
        m = max(m, e+1);
    }
    return m;
}
int main(){
    cin>>a>>b>>tag>>radix;
    if(tag == 2) swap(a,b);
    ll c = decimalNum(a, radix);
    ll lo = max(radixMin(b), 2LL);
    ll hi = max(lo, c)+1;
    ll t = 0, mi = 0;
    while(lo <= hi){
        mi = (lo+hi)/2;
        t = decimalNum(b, mi);
        if (t == c) break;
        else if(t > 0 && t < c) lo = mi + 1;
        else hi = mi-1;
    }
    t == c ? cout<<mi : cout<<"Impossible";
    return 0;
}