GoStack

Time limit1sMemory limit128 MB

Summary
Simulate a custom stack-based virtual machine with arithmetic operations and specific division rules, detecting errors and printing results for many inputs.
Level

Medium5 of 10

Topics
Stack, Simulation, Implementation
Solved
No attempts yet

Problem

Gochangyoung modified the stack a little and built the GoStack. A GoStack can only store integers and supports the following 10 operations.

For convenience, the number on the top of the stack is called the first number, the one below it the second number, then the third, and so on.

  • NUM X: Push XX onto the top of the stack. (0≤X≤1090 \le X \le 10^9)
  • POP: Remove the number on the top of the stack.
  • INV: Flip the sign of the first number. (e.g. 42→−4242 \rightarrow -42)
  • DUP: Duplicate the first number and push the copy onto the top of the stack.
  • SWP: Swap the positions of the first and second numbers.
  • ADD: Add the second number and the first number.
  • SUB: Subtract the first number from the second number. (second − first)
  • MUL: Multiply the second number and the first number.
  • DIV: Push the quotient of the second number divided by the first number. (the second number is the dividend, the first number is the divisor)
  • MOD: Push the remainder of the second number divided by the first number. (the second number is the dividend, the first number is the divisor)

For binary operations, the first number is the right operand and the second number is the left operand. When an operation runs, both numbers are popped from the stack and the result is pushed back.

Any of the following situations is a program error:

  • there are not enough numbers on the stack for the operation,
  • division by zero (DIV, MOD),
  • the absolute value of the result exceeds 10910^9.

To remove the ambiguity of dividing negative numbers, the following rule is used. If an operand is negative, take its absolute value before computing. Then decide the signs of the quotient and remainder as follows.

  • Sign of the quotient: negative if exactly one of the two operands is negative, and positive otherwise.
  • Sign of the remainder: the same as the sign of the dividend (the second number).

For example, 13÷(−4)=−313 \div (-4) = -3, (−13) mod 4=−1(-13) \bmod 4 = -1, and (−13) mod (−4)=−1(-13) \bmod (-4) = -1.

When a program error occurs, execution stops immediately and no further command is carried out.

Input

The input consists of the descriptions of several machines. Each machine description is split into a program and an input area.

The program is made of commands, one per line. Each command is one of the three-letter uppercase words described above, and no other characters appear. NUM is followed by a single integer separated by a space, and this integer is between 00 and 10910^9 inclusive. The program ends at the END line.

The first line of the input area contains the number of executions NN. (0≤N≤10,0000 \le N \le 10{,}000) Each of the next NN lines contains one input value ViV_i. (0≤Vi≤1090 \le V_i \le 10^9) The program is run once for each input value, and every run is independent. At the start of each run the stack contains only that input value ViV_i.

Machine descriptions are separated by a blank line. A QUIT line means there are no more machine descriptions. No single program has more than 100,000100{,}000 commands, and the stack never holds 1,0001{,}000 or more numbers during a run.

Output

For each input value, run the program and print its output value on its own line. The output value is the number left on the stack when the run finishes.

If a program error occurred, or the stack does not hold exactly one number when the run finishes, print ERROR instead.

Separate the outputs of different machines with a single blank line. Do not add a blank line after the last machine's output.

Examples3

  1. Example 1

    Input
    DUP
    MUL
    NUM 2
    ADD
    END
    3
    1
    10
    50
    
    NUM 1
    NUM 1
    ADD
    END
    2
    42
    43
    
    NUM 600000000
    ADD
    END
    3
    0
    600000000
    1
    
    QUIT
    
    Expected output
    3
    102
    2502
    
    ERROR
    ERROR
    
    600000000
    ERROR
    600000001
    
  2. Example 2

    Input
    NUM 5
    ADD
    END
    3
    0
    1000000000
    999999995
    
    INV
    END
    2
    1000000000
    0
    
    QUIT
    
    Expected output
    5
    ERROR
    1000000000
    
    -1000000000
    0
    
  3. Example 3

    Input
    NUM 4
    INV
    DIV
    END
    4
    13
    12
    0
    7
    
    NUM 4
    INV
    MOD
    END
    4
    13
    12
    0
    7
    
    INV
    NUM 4
    MOD
    END
    4
    13
    12
    0
    7
    
    INV
    NUM 4
    INV
    MOD
    END
    4
    13
    12
    0
    7
    
    QUIT
    
    Expected output
    -3
    -3
    0
    -1
    
    1
    0
    0
    3
    
    -1
    0
    0
    -3
    
    -1
    0
    0
    -3