zoukankan      html  css  js  c++  java
  • poj 2115 C Looooops

    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
    根据题意有如下等式:(a+cx)mod2^k=b;(x为循环的次数)故有a+c*x=b+y*2^k,c*x-y*2^k=b-a,就可以用扩展欧几里得定理了
    #include<stdio.h>
    #include<math.h>
    __int64 exgcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
    {
        if(b==0) {x=1;y=0;return a;}
        __int64 r=exgcd(b,a%b,x,y);
        __int64 temp=x;x=y;y=temp-(a/b)*y;
        return r;
    }
    int main()
    {
        __int64 a,b,c,k,x,y,m;
        while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF)
        {
            if(a==0&&b==0&&c==0&&k==0)
                break;
            m=pow(2.0,double(k));//我提交的时候,这里错了几次,pow()对参数有一定的要求
            __int64 r=exgcd(c,m,x,y);
            if((b-a)%r!=0) printf("FOREVER
    ");
            else {
                __int64 t=m/r;
                x=x*(b-a)/r;
                x=(x%t+t)%t;
                printf("%I64d
    ",x);
            }
        }
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    教你如何上传项目到GitHub
    Spring Boot日志使用
    Github库名命名规范
    failed to resolve org.junit.platform
    SecureCRT 关键字高亮显示
    curl 命令
    idea中展开折叠的文件夹
    python官网打不开
    小工具下载地址汇总
    Navicat12 for Mysql激活
  • 原文地址:https://www.cnblogs.com/duan-to-success/p/3486833.html
Copyright © 2011-2022 走看看