-
Notifications
You must be signed in to change notification settings - Fork 19
/
allaboutthatbase.java
98 lines (72 loc) · 2.46 KB
/
allaboutthatbase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.math.BigInteger;
import java.util.Scanner;
public class allaboutthatbase {
static BigInteger n1 , n2 , n3;
public static boolean eval(String num1 , String num2 , String num3 , String op , int base) {
if (base == 1)
{
if (!(num1.replaceAll("1", "").isEmpty() && num2.replaceAll("1", "").isEmpty() && num3.replaceAll("1", "").isEmpty()))
return false;
n1 = BigInteger.valueOf(num1.length());
n2 = BigInteger.valueOf(num2.length());
n3 = BigInteger.valueOf(num3.length());
}
else
{
if (base(base) - 1 < max(num1 , num2 , num3))
return false;
n1 = new BigInteger(num1 , base);
n2 = new BigInteger(num2 , base);
n3 = new BigInteger(num3 , base);
}
BigInteger max = BigInteger.valueOf(2).pow(32).subtract(BigInteger.ONE);
BigInteger one = BigInteger.ONE;
if (n1.compareTo(max) <= 0 && n1.compareTo(one) >= 0)
if (n2.compareTo(max) <= 0 && n2.compareTo(one) >= 0)
if (n3.compareTo(max) <= 0 && n3.compareTo(one) >= 0)
{
if (op.equals("+"))
return n1.add(n2).compareTo(n3) == 0;
if (op.equals("-"))
return n1.subtract(n2).compareTo(n3) == 0;
if (op.equals("*"))
return n1.multiply(n2).compareTo(n3) == 0;
BigInteger[] dr = n1.divideAndRemainder(n2);
return dr[1].compareTo(BigInteger.ZERO) == 0 && dr[0].compareTo(n3) == 0;
}
return false;
}
public static char base(int n) {
if (n == 36)
return (char)('z' + 4);
if (n < 10)
return (char)('0' + n);
return (char)('a' + (n - 10));
}
public static char max(String a , String b , String c) {
char max = '0';
String str = a + b + c;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) > max)
max = str.charAt(i);
return max;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
while (cases --> 0)
{
String num1 = scan.next();
String op = scan.next();
String num2 = scan.next();
scan.next();
String num3 = scan.next();
String bases = "";
for (int i = 1; i <= 36; i++)
if (eval(num1 , num2 , num3 , op , i))
bases += base(i);
System.out.println(bases.isEmpty() ? "invalid" : bases.replaceAll("~", "0"));
}
scan.close();
}
}