This page is still under construction.

Parts of this page are still being built. What you see may change.

C-Style Loops

Time limit1sMemory limit128 MB

Summary
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 kk-bit unsigned integer type modulo 2k2^k (that is, within the range 0≤x<2k0 \le x < 2^k, taking remainders modulo 2k2^k).

For the given AA, BB, CC, and kk, 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 AA, BB, CC, and kk separated by a single space. Here kk (1≤k≤321 \le k \le 32) is the number of bits of the loop control variable, and AA, BB, CC (0≤A,B,C<2k0 \le A, B, C < 2^k) 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 ii-th line contains the number of times statement is executed in the ii-th instance (a single integer), or FOREVER if the loop never terminates.

Examples1

  1. Example 1

    Input
    3 3 2 16
    3 7 2 16
    7 3 2 16
    3 4 2 16
    0 0 0 0
    
    Expected output
    0
    2
    32766
    FOREVER