1010. Radix (25)
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
1#include <iostream>2#include <string>3 4 using namespacestd;5 6 7 Long LongToradix (stringNumLong LongRadix)8 {9 Long Longres =0;Ten for(inti =0; I < num.size (); i++) One { A intx = (Num[i] >='0'&&num[i] <='9') ? Num[i]-'0': Num[i]-'a'+Ten; -res = Res*radix +x; - } the - returnRes; - } - + intMain () - { + stringnum[2]; A inttag; at Long LongRadix; - -CIN >> num[0] >> num[1] >> Tag >>Radix; - Long Long Base= Toradix (Num[tag-1], radix); - - stringNumber = num[2-tag]; in if(number.size () = =1) - { to intx = (number[0] >='0'&&number[0] <='9') ? number[0] -'0': number[0] -'a'+Ten; + if(x = =Base) -cout << x +1; the Else *cout <<"Impossible"; $ Panax Notoginseng return 0; - } the + Long LongMinradix =0, Maxradix =Base; A for(inti =0; I < number.size (); i++) the { + intx = (Number[i] >='0'&&number[i] <='9') ? Number[i]-'0': Number[i]-'a'+Ten; - if(x +1>Minradix) $Minradix = x +1; $ } - - while(Minradix <=Maxradix) the { - Long LongMiddle = (Minradix + maxradix)/2;Wuyi Long Longx =Toradix (number, middle); the if(x = =Base) - { Wucout <<Middle; - return 0; About } $ Else if(X >Base|| x<0) -Maxradix = Middle-1; - Else -Minradix = middle +1; A } + thecout <<"Impossible"; -}
PAT 1010. Radix (25)