• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            POJ 2115 模線性方程 ax=b(mod n)

            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


                推論1:方程ax=b(mod n)對于未知量x有解,當且僅當gcd(a,n) | b。
                推論2:方程ax=b(mod n)或者對模n有d個不同的解,其中d=gcd(a,n),或者無解。
                定理1:設d=gcd(a,n),假定對整數x和y滿足d=ax+by(比如用擴展Euclid算法求出的一組解)。如果d | b,則方程ax=b(mod n)有一個解x0滿足x0=x*(b/d) mod n 。特別的設e=x0+n,方程ax=b(mod n)的最小整數解x1=e mod (n/d),最大整數解x2=x1+(d-1)*(n/d)。
                定理2:假設方程ax=b(mod n)有解,且x0是方程的任意一個解,則該方程對模n恰有d個不同的解(d=gcd(a,n)),分別為:xi=x0+i*(n/d) mod n 。
                以上定理的具體證明見《算法導論》。
            #include <iostream>
            using namespace std;

            long long ext_gcd(long long a,long long b,long long &x,long long &y){
                
            long long t,ret;
                
            if(!b){
                    x
            =1,y=0;
                    
            return a;
                }

                ret
            =ext_gcd(b,a%b,x,y);
                t
            =x,x=y,y=t-a/b*y;
                
            return ret;
            }

            long long modular_linear(long long a,long long b,long long n){
                
            long long d,e,x,y;
                d
            =ext_gcd(a,n,x,y);
                
            if(b%d)
                    
            return -1;
                e
            =x*(b/d)%n+n;
                
            return e%(n/d);
            }

            int main(){
                
            long long d,a,b,c,k;
                
            while(scanf("%lld %lld %lld %lld",&a,&b,&c,&k),a||b||c||k){
                    d
            =modular_linear(c,b-a,1LL<<k);
                    
            if(d==-1)
                        puts(
            "FOREVER");
                    
            else
                        printf(
            "%lld\n",d);
                }

                
            return 0;
            }

            posted on 2009-06-12 19:24 極限定律 閱讀(2669) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導航

            統計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            一本一道久久综合狠狠老| 久久久久无码精品国产不卡| 中文字幕久久波多野结衣av| 香蕉久久av一区二区三区| 久久99精品国产麻豆宅宅| 国产香蕉97碰碰久久人人| 国产精品亚洲美女久久久| 久久人妻无码中文字幕| 青青草原综合久久大伊人精品| 久久青青草原精品国产不卡| 久久精品无码专区免费青青| 久久夜色撩人精品国产小说| 狠狠88综合久久久久综合网 | 婷婷久久久亚洲欧洲日产国码AV| 久久国产乱子伦免费精品| 人妻中文久久久久| 久久国产精品一区二区| 无码人妻久久久一区二区三区 | 亚洲综合熟女久久久30p| 国产亚洲美女精品久久久| 久久精品国产清高在天天线| 武侠古典久久婷婷狼人伊人| 99久久精品免费看国产免费| 久久人人爽人人爽人人AV东京热| 久久精品夜色噜噜亚洲A∨| 久久精品国产亚洲麻豆| 久久九九精品99国产精品| 久久综合久久综合亚洲| 久久精品国产只有精品66| 四虎国产永久免费久久| 久久99热精品| 国产L精品国产亚洲区久久| 久久男人Av资源网站无码软件| 久久精品国产亚洲AV影院| 久久精品国产99国产精品导航 | 亚洲精品无码久久久久AV麻豆| 国内精品久久久久久久亚洲| 久久精品国产99久久香蕉| 国内精品免费久久影院| 久久久久人妻精品一区三寸蜜桃| 91精品免费久久久久久久久|