Pat Class A exercises-Radix (25)-Two points find

Source: Internet
Author: User
Tags time limit

1010. Radix (+) time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue

Given a pair of positive integers, for example, 6 and +, can this equation 6 = + be true? The answer is ' yes ', if 6 is a decimal number and a binary number.

Now for any pair of positive integers N1 and N2, your task was to find the radix of one Iven.

Input Specification:

Each input file contains the one test case. Occupies a line which contains 4 positive integers:
N1 N2 Tag Radix
Here N1 and N2 each have no more than digits. A Digit is less than it 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, the equation N1 = N2 is true. If the equation is impossible, print "impossible". If The solution is not unique, the output of 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






/** Test Instructions: N1 n2 can be equal in some kind of binary, to find out that the binary tag = 1 radix for the N1 = 2 for the N2 of the input: the smallest of the system is definitely the largest digit +1 in N1, the largest system is not 26, but N1+1, said N2 with one can Represents N1 so it is not possible to use a For loop enumeration, but to use a binary lookup to note the range of data to use long long note that the N2 may overflow under X, the lookup area should be fixed to the left half */#include <bits/stdc++.h> using
namespace Std;
typedef pair<int,int> P;
typedef long Long LL;
const int MAXN = 2000+10;

const int INF = 0X3F3F3F3F;
    ll Change (char c) {if (c >= ' 0 ' && C <= ' 9 ') return C ' 0 ';
else return C ' a ' +10;
    } ll Getval (string S,int x) {///the value converted to decimal in the X-binary is ll res = 0;
        for (int i = 0;i<s.length (); ++i) {res *= x;
        Res + = Change (S[i]);

if (Res < 0) return-1;///here Note that res may overflow} return res;
    } int Main () {string n1,n2,t;
    int Tag,radix;
    Cin>> n1>>n2>>tag>>radix;
        if (tag = = 2) {///Let N1 always indicate the reference t = N1;
        N1 = n2;
    N2 = t;
    } ll N1val = Getval (N1,radix);
    ll low = 0, high, mid; for (int i = 0;i<n2.length (); ++i) {if(Change (N2[i]) > Low) low = Change (N2[i]);
    } low++;

    High = n1val+1;
    bool F = 0;
        while (low <= high) {mid = (Low+high)/2;
        ll midval = Getval (N2,mid);
        if (Midval < 0 | | midval > N1val) high = mid-1;
        else if (Midval < n1val) Low = mid +1;
            else if (Midval = N1val) {f = 1;
        Break
    }} if (f) cout << mid;

    else cout<< "impossible";

return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.