首页 > 代码库 > 扩展GCD exgcd

扩展GCD exgcd

已知 a,b (a>=0,b>=0)

求一组解 (x,y) 使得 (x,y)满足

gcd(a,b) = ax+by

注意求出的 x,y 可能为0或负数

代码中g = gcd(a,b);

ll extend_gcd (ll a , ll b , ll &x , ll &y) {  
    if (b == 0) {  
        x = 1LL;  
        y = 0;  
        return a;  
    }  
    ll g = extend_gcd (b , a % b , x , y);  
    ll t = x;  
    x = y; y = t - a / b * x;  
    return g;  
}