Блог пользователя 1507002

Автор 1507002, история, 6 лет назад, По-английски

We know that by means of extended euclidean algorithm x and y can be calculated from ax + by = gcd(a, b). The formula is:

x = prev_y;
y = prev_x - (a/b) * x;

and the code is:

int gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

But in case of multiple integers, for instance, ax + by + cz = gcd(a, b, c), what will be the approach and code?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +22 Проголосовать: не нравится

Let gcd(a, b) = d. We know how to solve for two numbers: d*X + c*Y = gcd(d, c). Note that gcd(d, c) = gcd(a, b, c). So now you have a solution that looks like gcd(a, b)*X + c*Y = gcd(a, b, c). Now just solve a*u + b*v = gcd(a, b), and you will get a*u*X + b*v*X + c*Y = gcd(a, b, c).