Premise: Given a specification for a “base” (well, actually a mixed radix number system), take in pairs of numbers written in our “base”, perform a specified operation on them and output the result in our base.
The Base: A number system where the right-most digit (digit 1) can be a counting number between 0 and 1, the second right-most digit (digit 2) can be a counting number between 0 and 2 and, more generally, each digit n (as labeled from the right) can have values between 0 and n. After 9, upper case letters are used, starting with A and going through Z. After the highest digit (which can be 0-Z), no further digits are possible; any numbers which go past that digit are invalid. Negative numbers are prefixed with a single “-” sign. Numbers never have leading zeros, with the exception of zero itself, which is represented by a single “0” character.
Operations: Addition (+) and subtraction (-): The numbers are added or subtracted as normal (including carrying, borrowing, etc).
//package Main;
import java.util.*;
import java.math.*;;
public class Main {
BigInteger[] digit = new BigInteger[40];
BigInteger A, B, C;
boolean flag = true;
void Getdigit() {
digit[1] = BigInteger.ONE;
for(int i=2;i<40;i++) {
digit[i] = digit[i-1].multiply(BigInteger.valueOf(i));
}
}
int GetVal(char c) {
if(c<=‘9‘&&c>=‘0‘)
return c-‘0‘;
else return c-‘A‘+10;
}
char RetVal(BigInteger i) {
if(i.intValue()<10)
return (char)(i.intValue()+‘0‘);
else return (char)(i.intValue()-10+(int)‘A‘);
}
BigInteger Check(String num) {
BigInteger ret = BigInteger.ZERO;
for(int i=num.length()-1,base = 2;i>=0;i--,base++) {
if(num.charAt(i)==‘-‘) break;
int tm = GetVal(num.charAt(i));
if(tm>=base)
{
flag = false;
return ret;
}
ret = ret.add(digit[base-1].multiply(BigInteger.valueOf(tm)));
}
if(ret.compareTo(digit[36])>=0) {
flag = false;
return ret;
}
if (num.charAt(0)==‘-‘) {
ret = BigInteger.ZERO.subtract(ret);
}
return ret;
}
void Print(BigInteger num) {
if(num.compareTo(BigInteger.ZERO)<0)
{
System.out.printf("-");
num = BigInteger.ZERO.subtract(num);
}
int i = 35;
for(;i>1;i--) {
if(num.divide(digit[i]).compareTo(BigInteger.ZERO)>0)
break;
}
for(;i>=1;i--) {
System.out.printf("%c", RetVal(num.divide(digit[i])));
num = num.mod(digit[i]);
}
System.out.println();
}
void main() {
Getdigit();
String num1, num2, op;
Scanner cin = new Scanner(System.in);
int cas = cin.nextInt();
for (int i = 0; i < cas; i++) {
flag = true;
num1 = cin.next();
op = cin.next();
num2 = cin.next();
A = Check(num1);
B = Check(num2);
if (!flag) {
System.out.println("Invalid");
continue;
}
if (op.charAt(0) == ‘+‘) {
C = A.add(B);
} else {
C = A.subtract(B);
}
if (C.abs().compareTo(digit[36]) >= 0) {
System.out.println("Invalid");
continue;
} else {
Print(C);
}
}
}
public static void main(String[] args) {
new Main().main();
}
}
/**************************************************************
Problem: 1003
User: YinJianZuiShuai
Language: Java
Result: Accepted
Time:196 ms
Memory:8424 kb
****************************************************************/