• poj 2115 C Looooops


    http://poj.org/problem?id=2115

    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20725   Accepted: 5598

    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
    扩展欧几里德的简单运用,由题意可得式子 

    b = (a + c * x) % 2k
    即:
    a + c * x = b(mod 2k
    
    
    即可以直接利用扩展欧几里德来求解x

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    ll r;
    
    void gcd(ll a, ll b, ll &x, ll &y)
    {
        ll t;
        if(b == 0)
        {
            x = 1;
            y = 0;
            r = a;
            return ;
        }
        gcd(b, a % b, x, y);
        t = x;
        x = y;
        y = t - a / b * y;
    }
    
    int main()
    {
        ll A, B, C, k, s;
        ll a, b, c, x, y;
        while(scanf("%lld%lld%lld%lld", &A, &B, &C, &k), A + B + C + k)
        {
            a = C;
            b = 1;
            for(ll i = 1 ; i <= k ; i++)
                b *= 2;
            c = B - A;
            gcd(a, b, x, y);
            if(c % r != 0)
            {
                printf("FOREVER
    ");
                continue;
            }
            s = b / r;
            x = c / r * x;
            x = (x % s + s) % s;
            printf("%lld
    ", x);
        }
        return 0;
    }
     
     
    
    
  • 相关阅读:
    Android SDK 在线更新镜像服务器
    Android Studio (Gradle)编译错误
    java ZIP压缩文件
    java文件操作(输出目录、查看磁盘符)
    JXL读取写入excel表格数据
    Linux命令zip和unzip
    Linux查看系统基本信息
    Ubuntu C++环境支持
    Linux开机执行bash脚本
    ubuntu中磁盘挂载与卸载
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4917259.html
Copyright © 2020-2023  润新知