#P2186. Magical Stack Machine

    ID: 15467 Type: Default 1000ms 256MiB

Magical Stack Machine

Magical Stack Machine

This problem involves simulating a magical stack machine that manipulates an integer stack using a set of operations. The machine supports the following operations (all formulas are given in \(\LaTeX\) format):

  • NUM x: Push the number \(x\) onto the stack.
  • POP: Remove the top element of the stack.
  • INV: Replace the top element \(a\) by \(-a\).
  • DUP: Duplicate the top element of the stack.
  • SWP: Swap the top two elements of the stack.
  • ADD: Pop the top two elements \(a\) and \(b\) and push \(b+a\).
  • SUB: Pop the top two elements \(a\) and \(b\) and push \(b-a\).
  • MUL: Pop the top two elements \(a\) and \(b\) and push \(b\times a\).
  • DIV: Pop the top two elements \(a\) and \(b\). If \(a=0\) then an error occurs. Otherwise, push the quotient computed as follows: Let \(q=\lfloor \frac{|b|}{|a|} \rfloor\) and the sign of the result is negative if and only if exactly one of \(a\) and \(b\) is negative. In other words, the result is \(\textrm{sign}(b,a)\,q\), where \(\textrm{sign}(b,a)= -1\) if \(b<0\) XOR \(a<0\) and \(+1\) otherwise.
  • MOD: Pop the top two elements \(a\) and \(b\). If \(a=0\) then an error occurs. Otherwise, let \(r = |b| \bmod |a|\) and the result has the same sign as \(b\); i.e. if \(b<0\) then push \(-r\) otherwise push \(r\).
  • END: Terminate the program.

The machine is used to implement a unary function \(f(x)\). Initially, the stack contains just one number \(x\). Then the program (a sequence of the above operations ending with END) is executed. If the execution completes normally, the stack should contain exactly one number which is considered as \(f(x)\). Otherwise, if during execution any of the following events occur, the machine produces an error:

  • The operation cannot be performed due to insufficient elements in the stack.
  • Division or modulo by zero occurs.
  • An intermediate or final value has an absolute value greater than \(10^9\).
  • The final stack does not contain exactly one number.

Your task is: Given the machine program and \(n\) queries (each query is an initial value \(x\)), compute \(f(x)\) for each query. If an error occurs during execution for a particular \(x\), output ERROR.

Note: All numbers in the operations and input are integers.

inputFormat

The input consists of a machine program followed by queries.

  1. The first part of the input is a sequence of commands (one per line). Each command is one of the 11 operations described above. The command END appears to mark the end of the program.
  2. The next line contains an integer \(n\) (\(1 \le n \le 10^4\)) representing the number of queries.
  3. Each of the following \(n\) lines contains an integer \(x\), which is the initial value in the stack.

There are no extra spaces in a command other than a single space between the operation and its argument (if any).

outputFormat

For each query, output a single line:

  • If the program terminates normally with exactly one number on the stack, output that number.
  • If any error occurs during execution, output ERROR (in uppercase).

sample

DUP
MUL
NUM 2
ADD
END
3
1
10
100000
3

102 ERROR

</p>