zoukankan      html  css  js  c++  java
  • POJ 2115 C Looooops



    C Looooops
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 14925Accepted: 3767

    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 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) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2k) 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 16
    3 7 2 16
    7 3 2 16
    3 4 2 16
    0 0 0 0

    Sample Output

    0
    2
    32766
    FOREVER

    Source


    扩展gcd:
    形如 ax≡n (mod b),可化成 ax + by = n, x有整数解的充分必要条件是 n % (a,b)==0,(a,b)表示gcd(a,b)
    方程 a*x+b*y=n;我们可以先用扩展欧几里德算法求出一组x_0,y_0。
    也就是 a * x_0 + b * y_0 =(a,b);
    然后两边同时除以(a,b),再乘以n。
    这样就得到了方程 a * x_0 * n/(a,b)+ b * y_0 * n/(a,b)= n;
    方程的一个解: x = x_0 * n/(a,b), y = y_0 * n/(a,b)
    其他的解都为 x_1,2...≡x+(b/(a,b))*i , y_1,2...≡y-(a/(a,b))*i (0 <=i<=(a,b)-1)。 i=0 时即为上面的一组解
    总共有gcd(a,b)组解

    令ff=b/(a,b),则x的最小非负整数解为 x = x_0 * n/(a,b), X = ( x % ff + ff ) % ff ;

     

    #include <iostream>
    #include <cstdio>

    using namespace std;

    typedef long long int LL;

    LL egcd(LL a,LL b,LL& x,LL& y)
    {
        if(b==0)
        {
            x=1;y=0;
            return a;
        }
        else
        {
            LL r=egcd(b,a%b,y,x);
            y-=x*(a/b);
            return r;
        }
    }

    int main()
    {
        LL A,B,C,k;

    while(cin>>A>>B>>C>>k)
    {
        if(A==0&&B==0&&C==0&&k==0) break;
        LL x0,y0,q,pow;
        pow=1LL<<k;
        q=egcd(C,pow,x0,y0);
        if((B-A)%q!=0) puts("FOREVER");
        else
        {
            LL x=x0*(B-A)/q;
            LL ff=pow/q;
            LL X=(x%ff+ff)%ff;
            cout<<X<<endl;
        }
    }

        return 0;
    }

  • 相关阅读:
    Javascript网页摇一摇
    移动端Web开发注意点
    Clappr——开源的Web视频播放器
    光看这图片就知道是大片--今天是五一劳动节尽管还是敲着代码(日常就是这样)然后想不出写什么了,也找不到好的素材,最后开心一下吧
    大放异彩的伪元素——可以做什么?(转)别人分享的文章,发现很不错,果断收藏了
    全屏滚动效果H5FullscreenPage.js
    今天我已无力吐槽了!写个没有营养的吐槽文。只是个人日记
    css的一些小技巧!页面视觉差!
    CSS3 transforms 3D翻开
    Javascript非构造函数的继承
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350930.html
Copyright © 2011-2022 走看看