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;
    }
    
  • 相关阅读:
    数据快照
    2.21毕设进度
    2.20毕设进度
    Java读取文件,明明文件存在,却报错文件找不到
    2.19毕设进度
    2.18毕设进度
    2.17毕设进度
    2.16毕设进度
    2.15毕设进度
    2.14毕设进度
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789741.html
Copyright © 2011-2022 走看看