#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:
- : Push onto the top of the stack.
- : Discard the top element of the stack.
- : Take out the top element and push its negation.
- : Push another number equal to the top element.
- : Swap the top two elements.
- : Pop the top two elements, add them, and push the result.
- : Pop the top two elements, subtract the first popped from the second popped, and push the result.
- : Pop the top two elements, multiply them, and push the result.
- : Pop the top two elements, integer-divide the second popped by the first popped, and push the result.
- : Pop the top two elements, take the second popped modulo the first popped, and push the result.
- : Terminate this program.
Then, Xiao Z used the above 11 operations to write a unary function . The value 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 .
Xiao Z has queries. For each value , he asks what 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 .
Also, because this machine is too fragile, if during the computation any absolute value exceeds , 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 . The function is guaranteed to end with .
Then an integer , the number of queries.
Then lines follow, each with an integer , representing a query for the value .
The input is not guaranteed to be valid.
Output Format
Output 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 , output
ERROR. - If the input data is invalid and causes an early exit, output
ERROR. - Otherwise, output the corresponding .
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 , , and .
Translated by ChatGPT 5
京公网安备 11011102002149号