#Q220. 「一本通 6.4 练习 4」C Looooops

「一本通 6.4 练习 4」C Looooops

Description

Original source: CTU Open 2004

For the C language loop statement:

for (variable = A; variable != B; variable += C)
  statement;

determine how many times the loop will execute before terminating in a kk-bit storage system. If it terminates within a finite number of iterations, output the loop count. Otherwise, output that it runs forever.

Input Format

Multiple test cases, each consisting of four integers A,B,C,kA, B, C, k on a single line. Here, kk denotes the kk-bit storage system.

The input ends with 0 0 0 0.

Output Format

If the loop terminates within a finite number of iterations, output the loop count. Otherwise, output FOREVER.

Sample 1

3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0

0
2
32766
FOREVER

Data Range and Hint

For all data, 1k321\le k\le 32, 0A,B,C<2k0\le A,B,C\lt 2^k.