1010 Radix (25分)

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 N​1​​ and N​2​​, 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<cmath>
#include<algorithm>
#define MAX 10000000
using namespace std;

// 任意进制转化为十进制
long long int radix_change(string n, long long int radix){

    long long int num_10 = 0;

    for(int i = 0; i < n.length(); i++){


        if(n[i] >= '0' && n[i] <= '9'){

            num_10 = num_10 * radix + n[i] - '0';
            

        }else if(n[i] >= 'a' && n[i] <= 'z'){

            num_10 = num_10 * radix + n[i] - 'a' + 10;
            
        }

    }

    return num_10;
}

long long int find_min_base(string n){

    long long int min_base = 0;
    long long int temp;
    for(int i = 0; i < n.length(); i++){


        if(n[i] >= '0' && n[i] <= '9'){

            temp = n[i] - '0';
            

        }else if(n[i] >= 'a' && n[i] <= 'z'){

            temp = n[i] - 'a' + 10;
            
        }

        min_base = max(min_base, temp);

    }

    return min_base + 1;
}

int search_base(long long int a, string n, long long int low, long long int high){

    if(low < 2) 
        low = 2;
    long long int left = low, right = high, middle = (low + high) / 2;
    while (left <= right)
    {
       long long int b = radix_change(n, middle);
       if(b > a || b < 0){

           right = middle - 1;

       }else if(b < a)
       {
           left = middle + 1;
       }else
       {
           return middle;
       }
       middle = (left + right) / 2;
       
    }
    
    return -1;
}


int main(){

    string n1,n2;
    int tag;
    long long int radix;
    long long int a;
    long long int min_base, max_base;
 
    cin >> n1 >> n2 >> tag >> radix;
    
    if(tag == 2)
        swap(n1, n2);
    a = radix_change(n1, radix);
    min_base = find_min_base(n2);
    max_base = max(a, min_base);
    int flag = search_base(a, n2, min_base, max_base);
    if(flag == -1)
         cout << "Impossible";
    else
    {
        cout << flag;
    }
    




   

    return 0;
}
发布了10 篇原创文章 · 获赞 0 · 访问量 66

猜你喜欢

转载自blog.csdn.net/zbchenchanghao/article/details/103938778