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

     
    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29034   Accepted: 8350

    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

     
    题意:k位存储元能存储2^k个二进制信息,题目即要求Cx==B-A (mod 2^k)的最小解x,这是线性模方程,其一般形式为ax==b(mod m),转化一下:
    设gcd(a.m)=G
    那么ax==b(mod m)就可以转化为
    (a/G)x==b/G (mod m/G) 继续转化
    x==(a/G)^(-1)*(b/G)(mod m/G)
    这样只要求出(a/G)的逆元,x即可求解
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<set>
    #include<string>
    #include<queue>
    #include<cmath>
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef long long LL;
    LL A, B, C, k;
    LL a, b, n;
    LL extgcd(LL a,LL b,LL &x,LL &y) {
        LL d = a;
        if (b != 0) {
            d = extgcd(b,a%b,y,x);
            y -= (a / b)*x;
        }
        else {
            x = 1; y = 0;
        }
        return d;
    }
    
    LL mod_inverse(LL a,LL m) {
        LL x, y;
        extgcd(a,m,x,y);
        return (m + x%m) % m;
    }
    
    LL gcd(LL a,LL b) {
        if (b == 0)return a;
        return gcd(b, a%b);
    }
    
    LL mod_fun(LL a,LL b,LL m,LL g) {
        return (b / g)*mod_inverse(a / g, m / g) % (m / g);
    }
    
    
    int main() {
        while (scanf("%lld%lld%lld%lld",&A,&B,&C,&k)&&(A||B||C||k)) {
            a = C;
            b = B - A;
            n = 1LL << k;
            LL g = gcd(a, n);
            if (b%g != 0) { puts("FOREVER"); continue; }
            if (b < 0)b += n;
            LL x = mod_fun(a, b, n, g);
            printf("%lld
    ",x);
        }
        return 0;
    }
     
  • 相关阅读:
    看了陈安之的文字 无论怎样 都要记住的是 你仍然是你自己 改变是应用他人的方法提高自己 改变是做更优秀更独特的自己
    流行的Ajax应用演示和源码下载(转)
    WEB2.0概念诠释(根据网络资料归纳)之一
    ASP.NET中文件上传下载方法集合(较为详细的介绍 转)
    建立梦想清单
    Ajax的应用
    Ajax的问题
    WEB2.0概念诠释(根据网络资料归纳)之三
    IIS突然挂掉
    VS2008软件90天过期,解决升级问题。
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/7790403.html
Copyright © 2011-2022 走看看