#P2186. 小 Z 的栈函数

小 Z 的栈函数

Description

Xiao Z recently found a magical machine whose operations are all done by maintaining a stack. It supports the following 11 operations:

  • NUM x\texttt{NUM} ~x: Push xx onto the top of the stack.
  • POP\texttt{POP}: Discard the top element of the stack.
  • INV\texttt{INV}: Take out the top element and push its negation.
  • DUP\texttt{DUP}: Push another number equal to the top element.
  • SWP\texttt{SWP}: Swap the top two elements.
  • ADD\texttt{ADD}: Pop the top two elements, add them, and push the result.
  • SUB\texttt{SUB}: Pop the top two elements, subtract the first popped from the second popped, and push the result.
  • MUL\texttt{MUL}: Pop the top two elements, multiply them, and push the result.
  • DIV\texttt{DIV}: Pop the top two elements, integer-divide the second popped by the first popped, and push the result.
  • MOD\texttt{MOD}: Pop the top two elements, take the second popped modulo the first popped, and push the result.
  • END\texttt{END}: Terminate this program.

Then, Xiao Z used the above 11 operations to write a unary function f(x)f(x). The value xx is the initial element pushed into the stack. After a sequence of operations, when the function ends, normally the stack will contain exactly one element. The remaining element is taken as the return value of f(x)f(x).

Xiao Z has nn queries. For each value xx, he asks what f(x)f(x) is as computed by the function above. However, the machine is too old and runs too slowly, and Xiao Z is impatient, so please write a program to help him compute his queries f(x)f(x).

Also, because this machine is too fragile, if during the computation any absolute value exceeds 10000000001000000000, the machine will fail.

Input Format

The input first contains several lines, each consisting of one of the 11 operations above, describing the operations of the function f(x)f(x). The function is guaranteed to end with END\texttt{END}.

Then an integer nn, the number of queries.

Then nn lines follow, each with an integer xx, representing a query for the value f(x)f(x).

The input is not guaranteed to be valid.

Output Format

Output nn lines, each representing the result of one query, following these rules:

  • If the stack does not end with exactly one element, output ERROR.
  • If during computation any absolute value exceeds 10000000001000000000, output ERROR.
  • If the input data is invalid and causes an early exit, output ERROR.
  • Otherwise, output the corresponding f(x)f(x).
NUM 600000000
ADD
END
3
0
600000000
1

600000000
ERROR
600000001

Hint

Constraints and Notes

For all test points, the number of function operations does not exceed 20002000, 1n20001 \leq n \leq 2000, and x109|x| \leq 10^{9}.

Translated by ChatGPT 5