zoukankan      html  css  js  c++  java
  • poj 2115 求线性同余方程 C Looooops(好理解欧几里德扩展定理怎么应用)

    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29061   Accepted: 8360

    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

    大致题意:

    对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。

    若在有限次内结束,则输出循环次数。

    否则输出死循环。

    解题思路:

    题意不难理解,只是利用了 k位存储系统 的数据特性进行循环。

    例如int型是16位的,那么int能保存2^16个数据,即最大数为65535(本题默认为无符号),

    当循环使得i超过65535时,则i会返回0重新开始计数

    如i=65534,当i+=3时,i=1

    其实就是 i=(65534+3)%(2^16)=1

    由此我们可以得到一个方程  A+CX = B(modn)n = 1 << k;

    即 CX = (B-A)% n;

    b = B-A;

    该方程有解的充要条件为 gcd(C,n) | b ,即 b% gcd(a,n)==0

    所以当b% gcd(C,n)!=0方程无解输出FOREVER

    然后再求b%gcd(C,n)为0时的最小x解

    令d = gcd(C,n)

    引入欧几里得扩展方程  d=Cx+by 

    (即最开始求CX+ny=1的方程解,最后再乘(b/d))  用欧几里德扩展定理求出x(最小解)与gcd(X,n)

    注意x0可能为负,因此要先 + n/d 再模n/d。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    typedef long long ll;
    int a,b,c,k;
    
    ll exgcd(ll a,ll b,ll &x,ll &y){
        if(b == 0){
            x = 1;
            y = 0;
            return a;
        }
        ll d = exgcd(b,a%b,x,y);
        ll tmp = x;
        x = y;
        y = tmp - a/b*y;
        return d;
    }
    
    ll quick_mod(ll a,ll b){
        ll ans = 1;
        while(b){
            if(b%2!=0){
                ans *= a;
                b --;
            }
            b /= 2;
            a *= a;
        }
        return ans;    
    }
    int main(){
        while(cin >> a >> b >> c >> k){
            if(!a && !b && !c && !k){
                break;
            }
            ll n = quick_mod(2,k);
            ll x,y;
            ll d = exgcd(c,n,x,y); //求a,n的最大公约数d=gcd(c,n)和方程d=cx+by的系数x、y  
            b = b - a;
            if(    b%d != 0){//方程 cx=b(mod n) 无解  
                cout << "FOREVER" << endl;
                continue;
            }
            x = (x*(b/d))%n;  //方程cx=b(mod n)的最小解  
            x = (x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最小正整数解  
            cout << x << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    ajax代码及简单封装
    web开发中不同设备浏览器的区分
    JS实现带复选框的下拉菜单
    常用浏览器的编码设置
    PHP实现实现数字补零格式化
    Linux杂碎2/SHELL
    OS
    Linux sudoers
    代理缓存服务器squid
    es6
  • 原文地址:https://www.cnblogs.com/l609929321/p/7799786.html
Copyright © 2011-2022 走看看