擴展歐幾里德算法詳解【轉載】

寫的是真的很好啊,大佬級文章
原地址:
http://blog.csdn.net/zhjchengfeng5/article/details/7786595

    擴展歐幾里德算法

    誰是歐幾里德?自己百度去

    先介紹什麼叫做歐幾里德算法

    有兩個數 a b,現在,我們要求 a b 的最大公約數,怎麼求?枚舉他們的因子?不現實,當 a b 很大的時候,枚舉顯得那麼的naïve ,那怎麼做?

    歐幾里德有個十分又用的定理: gcd(a, b) = gcd(b , a%b) ,這樣,我們就可以在幾乎是 log 的時間複雜度裏求解出來 a 和 b 的最大公約數了,這就是歐幾里德算法,用 C++ 語言描述如下:

    

    由於是用遞歸寫的,所以看起來很簡潔,也很好記憶。那麼什麼是擴展歐幾里德呢?

    現在我們知道了 a 和 b 的最大公約數是 gcd ,那麼,我們一定能夠找到這樣的 x 和 y ,使得: a*x + b*y = gcd 這是一個不定方程(其實是一種丟番圖方程),有多解是一定的,但是隻要我們找到一組特殊的解 x0 和 y0 那麼,我們就可以用 x0 和 y0 表示出整個不定方程的通解:

        x = x0 + (b/gcd)*t

        y = y0 – (a/gcd)*t

    爲什麼不是:

        x = x0 + b*t

        y = y0 – a*t

    這個問題也是在今天早上想通的,想通之後忍不住噴了自己一句弱逼。那是因爲:

    b/gcd 是 b 的因子, a/gcd 是 a 的因子是吧?那麼,由於 t的取值範圍是整數,你說 (b/gcd)*t 取到的值多還是 b*t 取到的值多?同理,(a/gcd)*t 取到的值多還是 a*gcd 取到的值多?那肯定又要問了,那爲什麼不是更小的數,非得是 b/gcd 和a/gcd ?

    注意到:我們令 B = b/gcd , A = a、gcd , 那麼,A 和 B 一定是互素的吧?這不就證明了 最小的係數就是 A 和 B 了嗎?要是實在還有什麼不明白的,看看《基礎數論》(哈爾濱工業大學出版社),這本書把關於不定方程的通解講的很清楚

    現在,我們知道了一定存在 x 和 y 使得 : a*x + b*y = gcd , 那麼,怎麼求出這個特解 x 和 y 呢?只需要在歐幾里德算法的基礎上加點改動就行了。

    我們觀察到:歐幾里德算法停止的狀態是: a= gcd , b = 0 ,那麼,這是否能給我們求解 x y 提供一種思路呢?因爲,這時候,只要 a = gcd 的係數是 1 ,那麼只要 b 的係數是 0 或者其他值(無所謂是多少,反正任何數乘以 0 都等於 0 但是a 的係數一定要是 1),這時,我們就會有: a*1 + b*0 = gcd

    當然這是最終狀態,但是我們是否可以從最終狀態反推到最初的狀態呢?

    假設當前我們要處理的是求出 a 和 b的最大公約數,並求出 x 和 y 使得 a*x + b*y= gcd ,而我們已經求出了下一個狀態:b 和 a%b 的最大公約數,並且求出了一組x1 和y1 使得: b*x1 + (a%b)*y1 = gcd , 那麼這兩個相鄰的狀態之間是否存在一種關係呢?

    我們知道: a%b = a - (a/b)*b(這裏的 “/” 指的是整除,例如 5/2=2 , 1/3=0),那麼,我們可以進一步得到:

        gcd = b*x1 + (a-(a/b)*b)*y1

            = b*x1 + a*y1 – (a/b)*b*y1

            = a*y1 + b*(x1 – a/b*y1)

    對比之前我們的狀態:求一組 x 和 y 使得:a*x + b*y = gcd ,是否發現了什麼?

    這裏:

        x = y1

        y = x1 – a/b*y1

    以上就是擴展歐幾里德算法的全部過程,依然用遞歸寫:

    

    依然很簡短,相比歐幾里德算法,只是多加了幾個語句而已。

    這就是理論部分,歐幾里德算法部分我們好像只能用來求解最大公約數,但是擴展歐幾里德算法就不同了,我們既可以求出最大公約數,還可以順帶求解出使得: a*x + b*y = gcd 的通解 x 和 y

    擴展歐幾里德有什麼用處呢?

    求解形如 a*x +b*y = c 的通解,但是一般沒有誰會無聊到讓你寫出一串通解出來,都是讓你在通解中選出一些特殊的解,比如一個數對於另一個數的乘法逆元

    什麼叫乘法逆元?

    

    這裏,我們稱 x 是 a 關於 m 的乘法逆元

    這怎麼求?可以等價於這樣的表達式: a*x + m*y = 1

    看出什麼來了嗎?沒錯,當gcd(a , m) != 1 的時候是沒有解的這也是 a*x + b*y = c 有解的充要條件: c % gcd(a , b) == 0

    接着乘法逆元講,一般,我們能夠找到無數組解滿足條件,但是一般是讓你求解出最小的那組解,怎麼做?我們求解出來了一個特殊的解 x0 那麼,我們用 x0 % m其實就得到了最小的解了。爲什麼?

可以這樣思考:

    x 的通解不是 x0 + m*t 嗎?

    那麼,也就是說, a 關於 m 的逆元是一個關於 m 同餘的,那麼根據最小整數原理,一定存在一個最小的正整數,它是 a 關於m 的逆元,而最小的肯定是在(0 , m)之間的,而且只有一個,這就好解釋了。

    可能有人注意到了,這裏,我寫通解的時候並不是 x0 + (m/gcd)*t ,但是想想一下就明白了,gcd = 1,所以寫了跟沒寫是一樣的,但是,由於問題的特殊性,有時候我們得到的特解 x0 是一個負數,還有的時候我們的 m 也是一個負數這怎麼辦?

    當 m 是負數的時候,我們取 m 的絕對值就行了,當 x0 是負數的時候,他模上 m 的結果仍然是負數(在計算機計算的結果上是這樣的,雖然定義的時候不是這樣的),這時候,我們仍然讓 x0 對abs(m) 取模,然後結果再加上abs(m) 就行了,於是,我們不難寫出下面的代碼求解一個數 a 對於另一個數 m 的乘法逆元:

    

還有最小整數解之類的問題,但都是大同小異,只要細心的推一推就出來了,這裏就不一一介紹了,下面給一些題目還有AC代碼,僅供參考

ZOJ 3609 :http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 求最小逆元

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <queue>  
#include <stack>  
#include <algorithm>  

#define INF 0x7fffffff  
#define EPS 1e-12  
#define MOD 1000000007  
#define PI 3.141592653579798  
#define N 100000  

using namespace std;  

typedef long long LL;  
typedef double DB;  

LL e_gcd(LL a,LL b,LL &x,LL &y)  
{  
    if(b==0)  
    {  
        x=1;  
        y=0;  
        return a;  
    }  
    LL ans=e_gcd(b,a%b,x,y);  
    LL temp=x;  
    x=y;  
    y=temp-a/b*y;  
    return ans;  
}  

LL cal(LL a,LL b,LL c)  
{  
    LL x,y;  
    LL gcd=e_gcd(a,b,x,y);  
    if(c%gcd!=0) return -1;  
    x*=c/gcd;  
    b/=gcd;  
    if(b<0) b=-b;  
    LL ans=x%b;  
    if(ans<=0) ans+=b;  
    return ans;  
}  

int main()  
{  
    LL a,b,t;  
    scanf("%lld",&t);  
    while(t--)  
    {  
        scanf("%lld%lld",&a,&b);  
        LL ans=cal(a,b,1);  
        if(ans==-1) printf("Not Exist\n");  
        else printf("%lld\n",ans);  
    }  
    return 0;  
}  

ZOJ 3593 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 求最小的步數,處理特殊一點就過去了

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <string>  
#include <vector>  
#include <stack>  
#include <queue>  
#include <algorithm>  

#define INF 0x7fffffff  
#define EPS 1e-12  
#define MOD 100000007  
#define PI 3.14159265357979823846  
#define N 100005  

using namespace std;  

typedef long long LL;  

LL e_gcd(LL a,LL b,LL &x,LL &y)  
{  
    if(b==0)  
    {  
        x=1;  
        y=0;  
        return a;  
    }  
    LL ans=e_gcd(b,a%b,x,y);  
    LL temp=x;  
    x=y;  
    y=temp-a/b*y;  
    return ans;  
}  
LL cal(LL a,LL b,LL L)  
{  
    LL x,y;  
    LL gcd=e_gcd(a,b,x,y);  
    if(L%gcd!=0) return -1;  
    x*=L/gcd;  
    y*=L/gcd;  
    a/=gcd;  
    b/=gcd;  
    LL ans=((LL)INF)*((LL)INF), f;  
    LL mid=(y-x)/(a+b);  
    for(LL T=mid-1;T<=mid+1;T++)  
    {  
        if(abs(x+b*T)+abs(y-a*T)==abs(x+b*T+y-a*T))  
            f=max(abs(x+b*T),abs(y-a*T));  
        else  
            f=fabs(x-y+(a+b)*T);  
        ans=min(ans,f);  
    }  
    return ans;  
}  

int main()  
{  
    //freopen("in.in","r",stdin);  
    //freopen("out.out","w",stdout);  
    LL A,B,a,b,x,y;  
    int t; scanf("%d",&t);  
    while(t--)  
    {  
        scanf("%lld%lld%lld%lld",&A,&B,&a,&b);  
        LL L=B-A;  
        LL ans=cal(a,b,L);  
        if(ans==-1) printf("-1\n");  
        else printf("%lld\n",ans);  
    }  
    return 0;  
}  

POJ 1061 http://poj.org/problem?id=1061 青蛙的約會,裸的擴展歐幾里得

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <queue>  
#include <stack>  
#include <algorithm>  

#define INF 0x7fffffff  
#define EPS 1e-12  
#define MOD 1000000007  
#define PI 3.141592653579798  
#define N 100000  

using namespace std;  

typedef long long LL;  
typedef double DB;  

LL e_gcd(LL a,LL b,LL &x,LL &y)  
{  
    if(b==0)  
    {  
        x=1;  
        y=0;  
        return a;  
    }  
    LL ans=e_gcd(b,a%b,x,y);  
    LL temp=x;  
    x=y;  
    y=temp-a/b*y;  
    return ans;  
}  

LL cal(LL a,LL b,LL c)  
{  
    LL x,y;  
    LL gcd=e_gcd(a,b,x,y);  
    if(c%gcd!=0) return -1;  
    x*=c/gcd;  
    b/=gcd;  
    if(b<0) b=-b;  
    LL ans=x%b;  
    if(ans<=0) ans+=b;  
    return ans;  
}  

int main()  
{  
    LL x,y,m,n,L;  
    while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF)  
    {  
        LL ans=cal(m-n,L,y-x);  
        if(ans==-1) printf("Impossible\n");  
        else printf("%lld\n",ans);  
    }  
    return 0;  
}  

HDU 1576 http://acm.hdu.edu.cn/showproblem.php?pid=1576 做點處理即可

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <queue>  
#include <stack>  
#include <algorithm>  

#define INF 0x7fffffff  
#define EPS 1e-12  
#define MOD 1000000007  
#define PI 3.141592653579798  
#define N 100000  

using namespace std;  

typedef long long LL;  
typedef double DB;  

LL e_gcd(LL a,LL b,LL &x,LL &y)  
{  
    if(b==0)  
    {  
        x=1;  
        y=0;  
        return a;  
    }  
    LL ans=e_gcd(b,a%b,x,y);  
    LL temp=x;  
    x=y;  
    y=temp-a/b*y;  
    return ans;  
}  

LL cal(LL a,LL b,LL c)  
{  
    LL x,y;  
    LL gcd=e_gcd(a,b,x,y);  
    if(c%gcd!=0) return -1;  
    x*=c/gcd;  
    b/=gcd;  
    if(b<0) b=-b;  
    LL ans=x%b;  
    if(ans<=0) ans+=b;  
    return ans;  
}  

int main()  
{  
    LL n,b,t;  
    scanf("%I64d",&t);  
    while(t--)  
    {  
        scanf("%I64d%I64d",&n,&b);  
        LL ans=cal(b,9973,n);  
        if(ans==-1) printf("Impossible\n");  
        else printf("%lld\n",ans);  
    }  
    return 0;  
}  

HDU 2669 http://acm.hdu.edu.cn/showproblem.php?pid=2669 裸的擴展歐幾里得

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

#define INF 0x7fffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 100000

using namespace std;

typedef long long LL;
typedef double DB;

LL e_gcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL ans=e_gcd(b,a%b,x,y);
    LL temp=x;
    x=y;
    y=temp-a/b*y;
    return ans;
}

LL cal(LL a,LL b,LL c)
{
    LL x,y;
    LL gcd=e_gcd(a,b,x,y);
    if(c%gcd!=0) return -1;
    x*=c/gcd;
    b/=gcd;
    if(b<0) b=-b;
    LL ans=x%b;
    if(ans<=0) ans+=b;
    return ans;
}

int main()
{
    LL a,b;
    while(scanf("%I64d%I64d",&a,&b)!=EOF)
    {
        LL ans=cal(a,b,1);
        if(ans==-1) printf("sorry\n");
        else printf("%I64d %I64d\n",ans,(1-ans*a)/b);
    }
    return 0;
}

暫時就這麼多了吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章