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;
    }
    
  • 相关阅读:
    本地YUM源制作
    VMware虚拟机三种联网方法及原理
    虚拟机安装centos
    Tomcat服务时区设置
    Tomcat的HTTPS配置及HTTP自动跳转配置
    应用程序下载地址汇总
    Centos 7 iptables配置
    JAVA 线程状态
    LeetCode Summary Ranges
    LeetCode Basic Calculator II
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789741.html
Copyright © 2011-2022 走看看