|
| 1 | +package Math.P21275; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static long X; |
| 11 | + static int A, B; |
| 12 | + static int[] n1, n2; |
| 13 | + |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + System.setIn(new FileInputStream("src/Math/P21275/input.txt")); |
| 16 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 18 | + |
| 19 | + String s1 = st.nextToken(), s2 = st.nextToken(); |
| 20 | + |
| 21 | + n1 = new int[s1.length()]; |
| 22 | + n2 = new int[s2.length()]; |
| 23 | + int A_min = 0, B_min = 0; |
| 24 | + |
| 25 | + for (int i = 0, size = s1.length(); i < size; i++) { |
| 26 | + char c = s1.charAt(size-i-1); |
| 27 | + n1[i] = (c >= 'a') ? c - 'a' + 10 : c - '0'; |
| 28 | + A_min = Math.max(A_min, n1[i]+1); |
| 29 | + } |
| 30 | + for (int i = 0, size = s2.length(); i < size; i++) { |
| 31 | + char c = s2.charAt(size-i-1); |
| 32 | + n2[i] = (c >= 'a') ? c - 'a' + 10 : c - '0'; |
| 33 | + B_min = Math.max(B_min, n2[i]+1); |
| 34 | + } |
| 35 | + |
| 36 | + int cnt = 0; |
| 37 | + loop: |
| 38 | + for (int a = A_min; a <= 36; a++) { |
| 39 | + long X1 = 0, a_ = 1; |
| 40 | + for (int i = 0, size = s1.length(); i < size; i++) { |
| 41 | + X1 += n1[i] * a_; |
| 42 | + a_ *= a; |
| 43 | + } |
| 44 | + for (int b = B_min; b <= 36; b++) { |
| 45 | + if (a == b) continue; |
| 46 | + long X2 = 0, b_ = 1; |
| 47 | + for (int i = 0, size = s2.length(); i < size; i++) { |
| 48 | + X2 += n2[i] * b_; |
| 49 | + b_ *= b; |
| 50 | + } |
| 51 | + |
| 52 | + if (X1 >= 0 && X1 == X2) { |
| 53 | + X = X1; |
| 54 | + A = a; |
| 55 | + B = b; |
| 56 | + if (++cnt > 1) break loop; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (cnt == 1) System.out.println(X + " " + A + " " + B); |
| 62 | + else if (cnt > 1) System.out.println("Multiple"); |
| 63 | + else System.out.println("Impossible"); |
| 64 | + } |
| 65 | +} |
0 commit comments