Program
InterviewTime limit1sMemory limit128 MB
Count the starting values from 1 to M that reach exactly A after running the given add, subtract, multiply, and floor-divide program.
- Level
Medium6 of 10
- Topics
- Binary search, Intervals, Simulation
- Solved
- No attempts yet
Problem
Hektor has recently started learning the programming language C--. A program in this language operates on a single natural-number variable that is stored in memory before any command is executed.
C-- provides the following commands:
+= X: adds to the variable in memory.-= X: subtracts from the variable in memory.*= X: multiplies the variable in memory by ./= X: divides the variable in memory by using integer division (rounding down).
Hektor wrote a C-- program and wants the value of the variable in memory to be exactly after the program finishes. How many initial values in the range make this possible?
Input
The first line contains the number of test cases ().
The first line of each test case contains the integers , , and (, , ). The next lines contain the commands of the program in order, where each satisfies .
You may assume that after every command the variable always stays within the signed 64-bit integer range and never becomes negative.
Output
For each test case, print on its own line how many initial values satisfy the condition described above.