C looooops
Time limit:1000 ms
Memory limit:65536kb
64bit Io format:% I64d & % i64usubmit status
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 and while variable is not equal to B, repeats statement followed by increasing the variable by C. we want to know how many times does the statement get executed for particle values of A, B and C, assuming that all arithmetics is calculated in a K-bit unsigned integer type (with values 0 <= x <2 k) modulo 2 K.
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) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, c <2 k) are the parameters of the loop.
The input is finished by a line containing four zeros.
Output
The output consists of several lines corresponding to the instances on the input. the I-th line contains either the number of executions of the statement in the I-th instance (a single integer number) or the word forever if the loop does not terminate.
Sample Input
3 3 2 163 7 2 167 3 2 163 4 2 160 0 0 0
Sample output
0232766 forever
Evaluate the knowledge of the Extended Euclidean Algorithm:
# Include <stdio. h> # include <math. h> long extended_euclid (long a, long B, long & X, long & Y) {If (B = 0) {x = 1; y = 0; return a; // d = A, x = 1, y = 0, then the equation d = AX + by is true} int d = extended_euclid (B, A % B, x, y); int XT = x; X = y; y = XT-A/B * Y; return D;} int main () {int, b, c, K; long a, B, c; long n; while (scanf ("% d", & A, & B, & C, & K )! = EOF) {if (a = 0 & B = 0 & C = 0 & K = 0) break; n = 1; for (INT I = 1; I <= K; I ++) {n = N * 2;} A = C; B = B-A; long X, Y; long long d = extended_euclid (A, n, x, y); If (B % d! = 0) // no solution {printf ("Forever \ n"); continue;} X = (x * (B/d) % N; X = (X % (n/D) + N/d) % (n/d); printf ("% LLD \ n", x) ;}return 0 ;}
Code written by others + parsing:
# Include <iostream> using namespace STD; // d = AX + by, where D = gcd (A, N) and X and Y are equation coefficients, the returned values are D, X, and Y _ int64 extended_euclid (_ int64 A ,__ int64 B ,__ int64 & X ,__ int64 & Y) {If (B = 0) {x = 1; y = 0; return a; // d = A, x = 1, y = 0, in this case, equation d = AX + by is true} _ int64 d = extended_euclid (B, A % B, x, y); _ int64 XT = x; X = y; y = XT-A/B * Y; // The values of X and Y must meet the equation d = AX + by return D;} int main (void) {_ int64 A, B, C, K; while (scanf ("% i64d % i64d % i64d % i64d", & A, & B ,& C, & K) {If (! A &&! B &&! C &&! K) break; _ int64 A = C; _ int64 B = B-A; _ int64 n = (_ int64) 1 <K; // 2 ^ K _ int64 X, Y; _ int64 d = extended_euclid (A, n, x, y); // evaluate, the maximum common divisor D of N = gcd (A, N) and equation d = coefficient x and y if (B % d! = 0) // equation Ax = B (mod n) unsolvable cout <"forever" <Endl; else {x = (x * (B/d) % N; // returns x = (X % (n/D) + N/d) % (n/d) of equation Ax = B (mod N ); // The smallest integer of equation Ax = B (mod n) printf ("% i64d \ n", x) ;}} return 0 ;}