C LooooopsTime
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u Submit Status Practice POJ 2115Descriptiona Compiler Mystery:we is 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 are not equal to B, repeats statement followed By increasing the variable by C. We want to know what many times does the statement get executed for particular values of A, B and C, assuming this all Arit Hmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2 k) modulo 2 K.
Inputthe input consists of several instances. Each instance are described by a A, a and four integers a, B, C and K separated by a single space. The integer k (1 <= k <=) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, c < 2 k) is the parameters of the loop.
The input is finished by a line containing four zeros. Outputthe 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
0232766FOREVER
Test Instructions: for (i=a;i!=b;i+=c) {i% (2^k)}; Q How many times do you loop through? idea: First assume that the equation is established: (a+x*c)% (2^k) =bVariant (2^k) *y+b=a+c*x ==> c*x+ (-(2^k) *y) =b-a;Ax+by=cSo now you know how to do it. Ha ha! Reprint Please specify source:http://www.cnblogs.com/yuyixingkong/ title Link:http://poj.org/problem?id=2115
#include <stdio.h>#defineLL unsigned long LongvoidEXGCD (LL a,ll b,ll& d,ll& x,ll&y) { if(!B) {d=a;x=1; y=0;} Else{EXGCD (b,a%b,d,y,x); Y-=x* (A/b); }}intMain () {LL a,b,c,k; while(SCANF ("%llu%llu%llu%llu", &a,&b,&c,&k), (a+b+c+k)) {LL a,b,c,d,x,y,dm; C=b-A; if(c==0) {printf ("0\n");Continue;} A=C; b= (LL)1<<K; EXGCD (A,b,d,x,y); if(c%d) {printf ("forever\n");Continue;} DM=b/D; X= (((X*C/D)%dm) +DM)%DM; printf ("%llu\n", x); } return 0;}
C looooops (poj2115+ extended Euclid)