• 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;
    }
     
  • 相关阅读:
    nginx能访问html静态文件但无法访问php文件
    LeetCode "498. Diagonal Traverse"
    LeetCode "Teemo Attacking"
    LeetCode "501. Find Mode in Binary Search Tree"
    LeetCode "483. Smallest Good Base" !!
    LeetCode "467. Unique Substrings in Wraparound String" !!
    LeetCode "437. Path Sum III"
    LeetCode "454. 4Sum II"
    LeetCode "445. Add Two Numbers II"
    LeetCode "486. Predict the Winner" !!
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/7790403.html
Copyright © 2020-2023  润新知