#P4041. [AHOI2014/JSOI2014] 奇怪的计算器
[AHOI2014/JSOI2014] 奇怪的计算器
Description
JYY’s calculator can execute preset instructions. Each time JYY inputs a positive integer , the calculator uses as the initial value, then executes the preset instructions in order, and finally returns the resulting value to JYY.
Each instruction is one of the following four types (here denotes a positive integer):
- : add to the current result.
- : subtract from the current result.
- : multiply the current result by .
- : add to the current result (where is the number JYY initially input).
The variable that stores the computation result has a limited range, so overflow may occur after each computation.
In JYY’s calculator, the result variable can only store positive integers between and . If after executing an instruction the result exceeds , the calculator will automatically change it to , then continue from . Similarly, if the result is less than , the calculator will change it to , then continue computing.
For example, suppose the calculator can store values from to . If the current result is , then after executing , the stored value will be . Although the actual result is , since exceeds the upper bound, the result is clamped to the upper bound .
JYY plans to input values into the calculator and wants to know what result each input will produce.
Input Format
The first line contains three positive integers , , and .
The next lines each contain one instruction. Each instruction, as described above, consists of one symbol and one positive integer, separated by a space.
The -th line contains an integer , the number of values JYY will input.
The next lines each contain a positive integer. The -th positive integer is the integer JYY inputs on the -th time.
Output Format
Output lines, each with a positive integer. The integer on the -th line is the result obtained after inputting and then executing the instructions in order.
5 1 6
+ 5
- 3
* 2
- 7
@ 2
3
2
1
5
5
3
6
Hint
Sample Explanation
When JYY inputs , the calculator performs operations. After each operation, the results are (the actual result is but it exceeds the upper bound), , , (the actual result is but it is below the lower bound), and (since the initial input was , this step computes ).
Constraints
For all testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号