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);
            }
        }
    }
  • 相关阅读:
    [Learn AF3]第二章 App Framework 3.0的组件View——AF3的驱动引擎
    [Learn AF3]第一章 如何使用App Framework 3.0 构造应用程序
    [译]Intel App Framework 3.0的变化
    手机浏览器中屏蔽img的系统右键菜单context menu
    HTML5 touche vents drag to move & AF actionsheet by longTap
    HTML5 FileReader
    【转】Gulp入门基础教程
    【Intel AF 2.1 学习笔记三】
    【Intel AF 2.1 学习笔记二】AF中的页面——Panel
    【Intel AF 2.1 学习笔记一】AF程序结构
  • 原文地址:https://www.cnblogs.com/candy99/p/5986102.html
Copyright © 2011-2022 走看看