zoukankan      html  css  js  c++  java
  • POJ2115 C Looooops[扩展欧几里得]

    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24355   Accepted: 6788

    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


    (A+s*C)%2^k=B
    (A+s*C)≡B(mod 2^k)
    s*C-m*2^k=B-A
    ax+by=c
    有一个问题,b没必要是负的,反正正负a和b的线性组合集都一样,况且此题不需要y
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    ll A,B,C,k;
    inline void exgcd(ll a,ll b,ll &g,ll &x,ll &y){
        if(b==0){x=1;y=0;g=a;}
        else{exgcd(b,a%b,g,y,x);y-=x*(a/b);}
    }
    int main(){
        while(scanf("%lld%lld%lld%lld",&A,&B,&C,&k)!=EOF){
            if(!A&&!B&&!C&&!k) break;
            ll c=B-A,a=C,b=1LL<<k,g,x,y;
            exgcd(a,b,g,x,y);
            if(c%g) printf("FOREVER
    ");
            else{
                b/=g;c/=g;
                printf("%lld
    ",(x%b*c%b+b)%b);
            }
        }
    }
  • 相关阅读:
    网页设计太麻烦?15款免费优质Bootstrap UI工具包助你效率倍增!
    【自古套路得人心】最全UI图表设计技巧和套路,分分钟打造最优设计
    原型对接工作流!摹客全面支持Axure原型
    赶紧收藏!41个Web UI工具包资源免费及付费下载
    2019年设计师必看,UI加载动画完全解读
    845G
    845G
    845D
    845C
    845B
  • 原文地址:https://www.cnblogs.com/candy99/p/5986102.html
Copyright © 2011-2022 走看看