1507002's blog

By 1507002, history, 6 years ago, In English

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?

  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
6 years ago, hide # |
 
Vote: I like it +22 Vote: I do not like it

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).