C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22704 Accepted: 6251
Description
A Compiler Mystery: We are given a C-language style for loop of type
for (variable = A; variable != B; variable += C)
statement;
I.e., a loop which starts by setting variable to value A andwhile variable isnotequalto B, repeats statement followed by increasing the variable by C. We want to know how many timesdoesthe statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k.
Input
The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) isthenumberof bits ofthe control variable ofthe loop and A, B, C (0 <= A, B, C < 2k) are the parameters ofthe loop.
The input is finished by a line containing four zeros.
Output
The output consists of several lines corresponding tothe instances onthe input. The i-th line contains either thenumberof executions ofthe statement inthe i-th instance (a single integernumber) ortheword FOREVER ifthe loop doesnot terminate.
Sample Input
332163721673216342160000
Sample Output
0232766
FOREVER
Source
CTU Open 2004