EXGCD板子 by ZaolyloaZ

· · 算法·理论

LL div_floor(LL a, LL b)
{
    if (a % b == 0 || a > 0 && b > 0 || a < 0 && b < 0)
        return a / b;
    else
        return a / b - 1;
}

bool extended_euclidean(LL a, LL b, LL c, LL& x, LL& y)
{
    LL quotient = 0;
    if (b == 0)
    {
        if (a == 0)
        {
            if (c == 0)
            {
                x = 0;
                y = 0;
                return true;
            }
            else
                return false;
        }
        else if (c % a != 0)
            return false;
        x = c / a;
        y = 0;
        return true;
    }
    if (!extended_euclidean(b, a % b, c, x, y))
        return false;
    x -= a / b * y;
    swap(x, y);
    quotient = div_floor(x, b);
    x -= quotient * b;
    y += quotient * a;
    return true;
}