C-Style Loops
Time limit1sMemory limit128 MB
Count the iterations of a C for loop whose counter wraps modulo 2^k, or report FOREVER if it never reaches the stopping value.
- Level
Medium5 of 10
- Topics
- Math, Number theory, Implementation, Simulation
- Solved
- No attempts yet
Problem
You are given a C-style for loop:
for (variable = A; variable != B; variable += C)
statement;
The loop initializes variable to A, then, while variable is not equal to B, repeatedly executes statement and increases variable by C. All arithmetic is performed on a -bit unsigned integer type modulo (that is, within the range , taking remainders modulo ).
For the given , , , and , determine how many times statement is executed. If the loop never terminates, print FOREVER instead.
Input
The input consists of several instances. Each instance is given on a single line containing four integers , , , and separated by a single space. Here () is the number of bits of the loop control variable, and , , () are the parameters of the loop.
The last line of the input contains four zeros; this line is not processed.
Output
Print one line for each instance. The -th line contains the number of times statement is executed in the -th instance (a single integer), or FOREVER if the loop never terminates.