扩展欧几里德

/*************************************************************************
    > File Name:        extend_gcd.cpp
    > Author:         wangzhili
    > Mail:           wangstdio.h@gmail.com
    > Created Time:   2014年03月17日 星期一 20时47分53秒
 ************************************************************************/
#include<iostream>          //求GCD(a, b), 同时求解满足条件的x, y, ax + by = GCD(a, b);
using namespace std;
int extend_gcd(int a, int b, int &x, int &y){
    if(b == 0){
        x = 1, y = 0;
        return a;
    }else{
        int r = extend_gcd(b, a%b, y, x);
        y -= x*(a/b);
        return r;
    }
}

int main(){
    int a, b, x, y;
    while(cin >> a >> b){
        int ans = extend_gcd(a, b, x, y);
        cout << ans << " " <<  x << " " << y << endl;
    }
    return 0;
}


posted on 2014-04-30 18:37  wangzhili  阅读(86)  评论(0)    收藏  举报