1003:all Your Base
Time Limit:1 Sec |
|
Memory limit:128 MB |
Submit:4 |
|
Solved:2 |
Description
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 is a counting number between 0 and 2 and, more generally, each digit n (as labeled from The right) can has the values between 0 and N. After 9, the upper case letters is used, starting with A and going through Z. After the highest digit (which can be 0-z), no further digits is possible; Any numbers which go past that digit is invalid. Negative numbers is prefixed with a single "-" sign. Numbers never has leading zeros, with the exception of zero itself, which are represented by a single "0" character.
Operations:addition (+) and subtraction (-): The numbers is added or subtracted as normal (including carrying, borrowing , etc).
Input
- The first line of input was the number (base) of operations that need to be performed.
- Each following line'll be is at most bytes and would consist of a variable-radix number, a space, a single character in Dicating the operation (+, or-), a space, another variable-radix number, and a newline (LF).
- Either number for any operation (and also the result) is negative.
Output
- For each operation in the input, a single line of output should is produced containing either the result (a Variable-radix Number) or the string "Invalid") (without quotes) followed by a newline (LF).
- If either of the input numbers or the resulting number is not valid in the number system, or a error is encountered while Performing the operation, the result is invalid.
Sample Input
+ 59987654321 + 1-a000000000-1
Sample Output
invalida000000000-a000000001
HINT Source
ACM ICPC South Central USA regional programming Contest C language almost write fried,/(ㄒoㄒ)/~~, or weak basic skills. Familiar with Java, to note that: the weight of each bit is calculated, is d[i] = d[i-1]*i; turn the string into a large integer, add and subtract the result, and then go back and remove the leading zeros. How to get the number, just divide by the weight d[i];
//Package Main; ImportJava.util.*; Importjava.math.*;; Public classMain {biginteger[] digit=NewBiginteger[40]; BigInteger A, B, C; BooleanFlag =true; voidGetdigit () {digit[1] =Biginteger.one; for(inti=2;i<40;i++) {Digit[i]= Digit[i-1].multiply (biginteger.valueof (i)); } } intGetval (Charc) {if(c<= ' 9 ' &&c>= ' 0 ') returnC ' 0 '; Else returnC ' A ' +10; } CharRetVal (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(intI=num.length () -1,base = 2;i>=0;i--, base++) { if(Num.charat (i) = = '-') Break; intTM =Getval (Num.charat (i)); if(tm>=base) {Flag=false; returnret; } ret= Ret.add (digit[base-1].multiply (Biginteger.valueof (tm))); } if(Ret.compareto (digit[36]) >=0) {flag=false; returnret; } if(Num.charat (0) = = '-') {ret=BigInteger.ZERO.subtract (ret); } returnret; } voidPrint (BigInteger num) {if(Num.compareto (Biginteger.zero) <0) {System.out.printf ("-"); Num=BigInteger.ZERO.subtract (num); } inti = 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 (); } voidMain () {getdigit (); String Num1, num2, op; Scanner Cin=NewScanner (system.in); intCAS =Cin.nextint (); for(inti = 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 voidMain (string[] args) {Newmain (). Main (); } } /************************************************************** problem:1003 User:yinjianzuishuai Language:ja VA result:accepted time:196 Ms memory:8424 KB****************************************************************/
Csuft 1003 All Your Base