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;
    }

  • 相关阅读:
    execl csv导出
    input里面check 状态检测
    注意这种方法的判断
    本周,上周,本月,上月
    bootstrap 兼容IE8设置
    js jquery 验证写法
    Jquery radio checked
    最简单的XML转数组
    eq,neq,gt,lt等表达式缩写
    python中的注释
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350930.html
Copyright © 2011-2022 走看看