Question information:
1010. Radix (25) Time Limit 400 MS
The memory limit is 32000 kb.
Code length limit: 16000 B
Criterion author 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
The Code is as follows:
# Include <iostream> # include <algorithm> # include <string> using namespace STD; long strtol (string & STR, long RDX) {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 '; elset = CH-'A' + 10; re + = T * n; N * = RDX;} return re;} int CMP (string STR, long RDX, long long N1) {long sum = 0; 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 '; elset = CH-'A' + 10; sum + = T * n; N * = RDX; If (sum> N1) return 1;} If (sum <N1) return-1; else if (sum> N1) return 1; elsereturn 0;} long binarysearch (string STR, long min, long Max, long N) {long mid = min; while (Min <= max) {int I = CMP (STR, mid, n); if (I = 0) return mid; else if (I = 1) max = mi D-1; elsemin = Mid + 1; Mid = (min + max)/2;} return-1;} int main () {string str1, str2; cin> str1> str2; long I, Radix; CIN> I> Radix; if (I = 2) {str1.swap (str2 );} long long n1 = strtol (str1, Radix); If (n1 = 1 & str2 = "1") // these two wonderful conditions !! {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; elseredx = CRDS-'A' + 10 + 1; int dx = (redx> N1 )? Redx: N1; I = binarysearch (str2, redx, dx, N1); if (I =-1) cout <"impossible" <Endl; elsecout <I <Endl; return 0 ;}
1010. Radix (25) -- Pat (Advanced Level) practise