zoukankan      html  css  js  c++  java
  • POJ-2115-C Looooops(线性同余方程)

    链接:

    https://vjudge.net/problem/POJ-2115

    题意:

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

    思路:

    求解(a+c*x equiv b (mod\,2^k)).
    转化(c*x + 2^k = b-a).
    扩展欧几里得求解.

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 1e6+10;
    
    LL a, b, c, k;
    
    LL ExGcd(LL a, LL b, LL &x, LL &y)
    {
        if (b == 0)
        {
            x = 1, y = 0;
            return a;
        }
        LL d = ExGcd(b, a%b, x, y);
        LL tmp = x;
        x = y;
        y = tmp - (a/b)*y;
        return d;
    }
    
    
    int main()
    {
        while(~scanf("%lld%lld%lld%lld
    ", &a, &b, &c, &k) && (a || b || c || k))
        {
            LL x, y;
            LL B = 1LL<<k;
            LL d = ExGcd(c, B, x, y);
            if ((b-a)%d != 0)
            {
                puts("FOREVER");
                continue;
            }
            x = x*(b-a)/d;
            x = (x%(B/d)+(B/d))%(B/d);
            printf("%lld
    ", x);
        }
    
        return 0;
    }
    
  • 相关阅读:
    Angularjs 中文版API v1.3.9 阅读
    jquery之ajax
    requirejs中 shim
    reqiurejs学习
    angularjs与require的集成摘抄
    安装go-admin
    多种GitHub加速方式
    git初始化仓库操作
    git cherry-pick
    gc问题定位
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789741.html
Copyright © 2011-2022 走看看