POJ 2115:c Looooops

Source: Internet
Author: User

C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19536 Accepted: 5204

Description

A 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 < 2k) modulo 2k.

Input

The 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 < 2k) is 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

0232766FOREVER

Test instructions is asking in

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

In this case, the loop number of times.

All of them have to be mod 2 for the K-th side. So the equation is (a+c*x)% (2^k) =b, Transformation is-c*x+ (2^k) *y=a-b. The smallest positive number x of the equation can be solved.

It is also an extension of Euclid.


Code:

#include <iostream> #include <string> #include <cstring> #include <algorithm> #include < Cmath>using namespace Std;long long yue;void ex_gcd (long long A,long long B, long long &xx,long long &yy) {if (b= =0) {xx=1;yy=0;yue=a;} ELSE{EX_GCD (B,A%B,XX,YY); Long long t=xx;xx=yy;yy=t-(A/b) *yy;}} int main () {Long Long a,b,c,k,k2,xx,yy;while (scanf_s ("%lld%lld%lld%lld", &a,&b,&c,&k)) {if (! a&&! b&&! c&&!k) break;k2= (1ll<<k); EX_GCD (-C,K2,XX,YY); if ((a)%yue) {cout<< "FOREVER" <<ENDL;} Else{xx=xx* (A-B)/yue), Long Long r=k2/yue;if (r<0) xx= (xx%r-r)%r;elsexx= (xx%r+r)%r;printf ("%lld\n", XX);}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2115:c Looooops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.