Post · 7 min read· pp. 21–26

Linear Diophantine Equation

A Linear Diophantine Equation (in two variables) is an equation of the general form:

ax+by=cax + by = c

where aa, bb, cc are given integers, and xx, yy are unknown integers.

In this article, we consider several classical problems on these equations:

  • finding one solution
  • finding all solutions
  • finding the number of solutions and the solutions themselves in a given interval
  • finding a solution with minimum value of x+yx + y

The degenerate case

A degenerate case that needs to be taken care of is when a=b=0a = b = 0. It is easy to see that we either have no solutions or infinitely many solutions, depending on whether c=0c = 0 or not. In the rest of this article, we will ignore this case.

Analytic solution

When a≠0a \neq 0 and b≠0b \neq 0, the equation ax+by=cax+by=c can be equivalently treated as either of the following:

ax≡c(modb)by≡c(moda)\begin{align} ax &\equiv c \pmod b \\ by &\equiv c \pmod a \end{align}

Without loss of generality, assume that b≠0b \neq 0 and consider the first equation. When aa and bb are co-prime, the solution to it is given as

x≡ca−1(modb),x \equiv ca^{-1} \pmod b,

where a−1a^{-1} is the modular inverse of aa modulo bb.

When aa and bb are not co-prime, values of axax modulo bb for all integer xx are divisible by g=gcd⁡(a,b)g=\gcd(a, b), so the solution only exists when cc is divisible by gg. In this case, one of solutions can be found by reducing the equation by gg:

(a/g)x≡(c/g)(modb/g).(a/g) x \equiv (c/g) \pmod{b/g}.

By the definition of gg, the numbers a/ga/g and b/gb/g are co-prime, so the solution is given explicitly as

{x≡(c/g)(a/g)−1(modb/g),y=c−axb.\begin{cases} x \equiv (c/g)(a/g)^{-1}\pmod{b/g},\\ y = \frac{c-ax}{b}. \end{cases}

Algorithmic solution

Bézout's lemma (also called Bézout's identity) is a useful result that can be used to understand the following solution.

Let g=gcd⁡(a,b)g = \gcd(a,b). Then there exist integers x,yx,y such that ax+by=gax + by = g.

Moreover, gg is the least such positive integer that can be written as ax+byax + by; all integers of the form ax+byax + by are multiples of gg.

To find one solution of the Diophantine equation with 2 unknowns, you can use the Extended Euclidean algorithm. First, assume that aa and bb are non-negative. When we apply Extended Euclidean algorithm for aa and bb, we can find their greatest common divisor gg and 2 numbers xgx_g and ygy_g such that:

axg+byg=ga x_g + b y_g = g

If cc is divisible by g=gcd⁡(a,b)g = \gcd(a, b), then the given Diophantine equation has a solution, otherwise it does not have any solution. The proof is straight-forward: a linear combination of two numbers is divisible by their common divisor.

Now suppose that cc is divisible by gg, then we have:

a⋅xg⋅cg+b⋅yg⋅cg=ca \cdot x_g \cdot \frac{c}{g} + b \cdot y_g \cdot \frac{c}{g} = c

Therefore one of the solutions of the Diophantine equation is:

x0=xg⋅cg,x_0 = x_g \cdot \frac{c}{g},
y0=yg⋅cg.y_0 = y_g \cdot \frac{c}{g}.

The above idea still works when aa or bb or both of them are negative. We only need to change the sign of x0x_0 and y0y_0 when necessary.

Finally, we can implement this idea as follows (note that this code does not consider the case a=b=0a = b = 0):

text
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;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
    g = gcd(abs(a), abs(b), x0, y0);
    if (c % g) {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;
    return true;
}

Getting all solutions

From one solution (x0,y0)(x_0, y_0), we can obtain all the solutions of the given equation.

Let g=gcd⁡(a,b)g = \gcd(a, b) and let x0,y0x_0, y_0 be integers which satisfy the following:

a⋅x0+b⋅y0=ca \cdot x_0 + b \cdot y_0 = c

Now, we should see that adding b/gb / g to x0x_0, and, at the same time subtracting a/ga / g from y0y_0 will not break the equality:

a⋅(x0+bg)+b⋅(y0−ag)=a⋅x0+b⋅y0+a⋅bg−b⋅ag=ca \cdot \left(x_0 + \frac{b}{g}\right) + b \cdot \left(y_0 - \frac{a}{g}\right) = a \cdot x_0 + b \cdot y_0 + a \cdot \frac{b}{g} - b \cdot \frac{a}{g} = c

Obviously, this process can be repeated again, so all the numbers of the form:

x=x0+k⋅bgx = x_0 + k \cdot \frac{b}{g}
y=y0−k⋅agy = y_0 - k \cdot \frac{a}{g}

are solutions of the given Diophantine equation.

Since the equation is linear, all solutions lie on the same line, and by the definition of gg this is the set of all possible solutions of the given Diophantine equation.

Finding the number of solutions and the solutions in a given interval

From previous section, it should be clear that if we don't impose any restrictions on the solutions, there would be infinite number of them. So in this section, we add some restrictions on the interval of xx and yy, and we will try to count and enumerate all the solutions.

Let there be two intervals: [minx;maxx][min_x; max_x] and [miny;maxy][min_y; max_y] and let's say we only want to find the solutions in these two intervals.

Note that if aa or bb is 00, then the problem only has one solution. We don't consider this case here.

First, we can find a solution which has minimum value of xx, such that x≥minxx \ge min_x. To do this, we first find any solution of the Diophantine equation. Then, we shift this solution to get x≥minxx \ge min_x (using what we know about the set of all solutions in previous section). This can be done in O(1)O(1). Denote this minimum value of xx by lx1l_{x1}.

Similarly, we can find the maximum value of xx which satisfies x≤maxxx \le max_x. Denote this maximum value of xx by rx1r_{x1}.

Similarly, we can find the minimum value of yy (y≥miny)(y \ge min_y) and maximum value of yy (y≤maxy)(y \le max_y). Denote the corresponding values of xx by lx2l_{x2} and rx2r_{x2}.

The final solution is all solutions with x in intersection of [lx1,rx1][l_{x1}, r_{x1}] and [lx2,rx2][l_{x2}, r_{x2}]. Let denote this intersection by [lx,rx][l_x, r_x].

Following is the code implementing this idea. Notice that we divide aa and bb at the beginning by gg. Since the equation ax+by=ca x + b y = c is equivalent to the equation agx+bgy=cg\frac{a}{g} x + \frac{b}{g} y = \frac{c}{g}, we can use this one instead and have gcd⁡(ag,bg)=1\gcd(\frac{a}{g}, \frac{b}{g}) = 1, which simplifies the formulas.

text
void shift_solution(int & x, int & y, int a, int b, int cnt) {
    x += cnt * b;
    y -= cnt * a;
}

int find_all_solutions(int a, int b, int c, int minx, int maxx, int miny, int maxy) {
    int x, y, g;
    if (!find_any_solution(a, b, c, x, y, g))
        return 0;
    a /= g;
    b /= g;

    int sign_a = a > 0 ? +1 : -1;
    int sign_b = b > 0 ? +1 : -1;

    shift_solution(x, y, a, b, (minx - x) / b);
    if (x < minx)
        shift_solution(x, y, a, b, sign_b);
    if (x > maxx)
        return 0;
    int lx1 = x;

    shift_solution(x, y, a, b, (maxx - x) / b);
    if (x > maxx)
        shift_solution(x, y, a, b, -sign_b);
    int rx1 = x;

    shift_solution(x, y, a, b, -(miny - y) / a);
    if (y < miny)
        shift_solution(x, y, a, b, -sign_a);
    if (y > maxy)
        return 0;
    int lx2 = x;

    shift_solution(x, y, a, b, -(maxy - y) / a);
    if (y > maxy)
        shift_solution(x, y, a, b, sign_a);
    int rx2 = x;

    if (lx2 > rx2)
        swap(lx2, rx2);
    int lx = max(lx1, lx2);
    int rx = min(rx1, rx2);

    if (lx > rx)
        return 0;
    return (rx - lx) / abs(b) + 1;
}

Once we have lxl_x and rxr_x, it is also simple to enumerate through all the solutions. Just need to iterate through x=lx+k⋅bgx = l_x + k \cdot \frac{b}{g} for all k≥0k \ge 0 until x=rxx = r_x, and find the corresponding yy values using the equation ax+by=ca x + b y = c.

Find the solution with minimum value of x+yx + y

Here, xx and yy also need to be given some restriction, otherwise, the answer may become negative infinity.

The idea is similar to previous section: We find any solution of the Diophantine equation, and then shift the solution to satisfy some conditions.

Finally, use the knowledge of the set of all solutions to find the minimum:

x′=x+k⋅bg,x' = x + k \cdot \frac{b}{g},
y′=y−k⋅ag.y' = y - k \cdot \frac{a}{g}.

Note that x+yx + y change as follows:

x′+y′=x+y+k⋅(bg−ag)=x+y+k⋅b−agx' + y' = x + y + k \cdot \left(\frac{b}{g} - \frac{a}{g}\right) = x + y + k \cdot \frac{b-a}{g}

If a<ba < b, we need to select smallest possible value of kk. If a>ba > b, we need to select the largest possible value of kk. If a=ba = b, all solution will have the same sum x+yx + y.

Practice Problems