Poj2115 -- C looooops (Extended gcd)

Source: Internet
Author: User
C looooops
Time limit:1000 ms   Memory limit:65536 K
Total submissions:17740   Accepted:4600

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

0232766FOREVER

Source

The initial value of the question is a. If you add c at each time, you can modulo 2 ^ K for the obtained value. When the value is B, the question will be asked at least how many times, if you have never stopped forever

From the question to the equation, if it is certain to stop, then after the loop X times, it will be equal to B (A + x * C) % (2 ^ K) = B. Obviously, expand the GCD question and obtain the equation. A + C * X-B = y * (2 ^ K), that is

Solve C * X-(2 ^ K) * Y = B-a and ask if there is any integer solution.

For a * X1 + B * Y1 = C, use the extension GCD to solve a * X1 + B * Y1 = GAD (a, B) = gcd (B, A % B) = A * y2 + B * (x2-a/B * Y2) to get X1 = Y2, Y1 = (X2 + A/B * Y2), using this equation, it can be pushed to B = 0, that is x = 1, y = 0, and the maximum number of public approx. A is returned layer by layer, and a * X1 + B * Y1 = GAD (, b). If C % (gcd (a, B) = 0, then a * X1 + B * Y1 = C has an integer, so that D = gcd (A, B, b), D = B/d. the smallest positive integer excludes x = (X % d + d) % d.

 

#include <cstdio>#include <cstring>#include <algorithm>#define LL __int64using namespace std;void gcd(LL a,LL b,LL &d,LL &x,LL &y){    if(b == 0)    {        d = a ;        x = 1 ;        y = 0 ;    }    else    {        gcd(b,a%b,d,y,x);        y = -y ; x = -x ;        y += a/b*x ;    }    return ;}int main(){    LL k , a , b , c , d , x , y ;    while(scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &k)!=EOF)    {        if(a == 0 && b == 0 && c == 0 && k == 0)            break;        k = ( (LL)1 )<<k ;        gcd(c,k,d,x,y);        if( (b-a)%d )            printf("FOREVER\n");        else        {            x = (b-a)/d*x ;            k = k/d ;            x = (x%k+k)%k ;            printf("%I64d\n", x);        }    }    return 0;}


 

 

Related Article

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.