Exgcd

扩展欧几里得定理

https://blog.csdn.net/zhjchengfeng5/article/details/7786595?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

ax + by = gcd(a, b),已知a,b,求x, y。

上述方程一定有解。等式右面是为 c 保证 c % gcd(a, b) == 0 则有解。

板子:返回值是 a 和 b 的最大公因数

ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if(!b)
    {
        x = 1, y = 0;
        return a;
    }
    int d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
原文地址:https://www.cnblogs.com/Maxx-el/p/14119354.html