扩展欧几里得

扩展欧几里得

解释:

扩展欧几里得算法是用来在已知 a, b 的情况下求解一组 x,y,使它们满足等式ax+by=gcd(a,b)=kd(注意:gcd(a, b) | d)该方程的解一定存在

证明:略

模板代码:

#include<iostream>
#include<cstdio>
using namespace std;

typedef long long LL; 

void extend_Eulid(LL a, LL b, LL &x, LL &y, LL &d){
    if (b == 0) {d = a, x = 1, y = 0;}
    else{
        extend_Eulid(b, a % b, y, x, d);
        y -= x * (a / b);
    }
}

int main(){
    LL a, b, d, x, y;
    while(~scanf("%lld%lld", &a, &b)){
        extend_Eulid(a, b, x, y, d);
        printf("%lld*a + %lld*b = %lld\n", x, y, d);
    }
}
posted @ 2018-11-24 15:56  fishers  阅读(797)  评论(0编辑  收藏  举报