MATH2400 2,201 words·12 min read

Linear Congruences and Diophantine Equations

Linear Congruences#

Recall from Bezouts Identity and the Extended Euclidean Algorithm the method for finding the inverse of an integer aa in Zn\mathbb{Z}_n:

  • Find gcd⁡(a,n)\gcd(a,n). If it is not 11, there is no inverse.
  • Find integers xx and yy such that 1=ax+ny1 = ax + ny, e.g. via the extended Euclidean algorithm.
  • The inverse of aa in Zn\mathbb{Z}_n is the value of the coefficient xx, reduced modulo nn.

That method solves the congruence ax≡1(modn)ax \equiv 1 \pmod n. The natural next question is the more general linear congruence

ax≡c(modn),ax \equiv c \pmod n,

for given integers aa and cc and positive integer nn; i.e. instead of asking "what undoes aa", we are asking "what does aa send to cc". We will look at two hands-on approaches first, and then build a general method that runs on the same EEA machinery as the inverse method.

Checking All Multiples#

This one is just brute force, so I'm not gonna say much; since xx only matters modulo nn, there are only nn candidates to test, namely x∈{0,1,…,n−1}x \in \{0, 1, \dots, n-1\}.

Example. Find all solutions to the linear congruence 6x≡4(mod7)6x \equiv 4 \pmod 7.
Testing every residue modulo 77:

xx 00 11 22 33 44 55 66
6x mod 76x \bmod 7 00 66 55 44 33 22 11

The only residue that works is x=3x = 3, since 6×3=18=2×7+46 \times 3 = 18 = 2 \times 7 + 4. Therefore, the congruence has exactly one solution, x≡3(mod7)x \equiv 3 \pmod 7.

Example. Find all solutions to the linear congruence 6x≡4(mod8)6x \equiv 4 \pmod 8.
Testing every residue modulo 88:

xx 00 11 22 33 44 55 66 77
6x mod 86x \bmod 8 00 66 44 22 00 66 44 22

This time two residues work. Therefore, the solutions are x≡2x \equiv 2 and x≡6(mod8)x \equiv 6 \pmod 8; notice that these can be packaged as the single statement x≡2(mod4)x \equiv 2 \pmod 4.

Example. Find all solutions to the linear congruence 6x≡4(mod9)6x \equiv 4 \pmod 9.
Testing every residue modulo 99:

xx 00 11 22 33 44 55 66 77 88
6x mod 96x \bmod 9 00 66 33 00 66 33 00 66 33

The outputs only ever cycle through {0,3,6}\{0, 3, 6\} and never hit 44. Therefore, this congruence has no solutions.

Notice how the same left-hand side against three different moduli produced one, two and zero solutions. Look at which values each table can reach: for n=7n=7 we hit everything, for n=8n=8 we only hit the even residues, and for n=9n=9 we only hit the multiples of 33. In each case the reachable values are exactly the multiples of gcd⁡(6,n)\gcd(6,n) (which is 11, 22 and 33 respectively); this observation is basically the entire theory of linear congruences, and we will state it as a theorem shortly.

Using Rules of Modular Arithmetic#

Brute force dies quickly as nn grows, so the second approach is to massage the congruence using the standard rules of modular arithmetic; adding multiples of the modulus to either side, replacing numbers by anything congruent to them, and cancelling common factors (carefully).

Example. Solve the same three congruences again, this time using rules of modular arithmetic.
For the modulus 77, notice that 6≡−1(mod7)6 \equiv -1 \pmod 7, so

6x≡4(mod7)−x≡4(mod7)x≡−4(mod7)x≡3(mod7).\begin{align*} 6x &\equiv 4 \pmod 7 \\ -x &\equiv 4 \pmod 7 \\ x &\equiv -4 \pmod 7 \\ x &\equiv 3 \pmod 7. \end{align*}

For the modulus 88, every term and the modulus share a factor of 22, so we may divide the whole congruence (modulus included) through by 22, and then use 3≡−1(mod4)3 \equiv -1 \pmod 4:

6x≡4(mod8)3x≡2(mod4)−x≡2(mod4)x≡−2(mod4)x≡2(mod4),\begin{align*} 6x &\equiv 4 \pmod 8 \\ 3x &\equiv 2 \pmod 4 \\ -x &\equiv 2 \pmod 4 \\ x &\equiv -2 \pmod 4 \\ x &\equiv 2 \pmod 4, \end{align*}

which is x≡2x \equiv 2 or 6(mod8)6 \pmod 8, agreeing with the table.
For the modulus 99, suppose a solution existed. Then 9∣6x−49 \mid 6x - 4, i.e. 6x−9k=46x - 9k = 4 for some integer kk. But 3∣6x3 \mid 6x and 3∣9k3 \mid 9k, so 33 divides the left-hand side, forcing 3∣43 \mid 4; this is false, so there are no solutions. Therefore, the three answers match the brute-force tables, with far less work.

It is important to remember that you can only cancel a common factor from both sides of a congruence if you also divide the modulus by the gcd of that factor and the modulus. Cancelling as if the modulus were untouched is probably the single most common error in this topic; here is what goes wrong.

Example. Find all solutions to 4x≡8(mod12)4x \equiv 8 \pmod{12}.
The tempting (wrong) move is to cancel the 44 and write x≡2(mod12)x \equiv 2 \pmod{12}. But check x=5x = 5:

4×5=20=12+8≡8(mod12),\begin{align*} 4 \times 5 &= 20 \\ &= 12 + 8 \\ &\equiv 8 \pmod{12}, \end{align*}

so x=5x = 5 is a perfectly good solution that the naive answer misses. The correct move: the factor being cancelled is 44, and gcd⁡(4,12)=4\gcd(4, 12) = 4, so the modulus must be divided by 44 as well;

4x≡8(mod12)x≡2(mod3).\begin{align*} 4x &\equiv 8 \pmod{12} \\ x &\equiv 2 \pmod 3. \end{align*}

As residues modulo 1212, this is x≡2,5,8,11(mod12)x \equiv 2, 5, 8, 11 \pmod{12}. Therefore, there are four solutions modulo 1212, and the naive cancellation silently threw away three of them.

Existence and Number of Solutions#

The pattern spotted in the tables above is completely general.

Note

Theorem (Existence of Solutions to Linear Congruences)
Let a,c∈Za, c \in \mathbb{Z} and n∈Z+n \in \mathbb{Z}^+, and let d=gcd⁡(a,n)d = \gcd(a,n). The linear congruence

ax≡c(modn)ax \equiv c \pmod n

has a solution if and only if d∣cd \mid c. When d∣cd \mid c, the solutions form exactly one congruence class modulo nd\frac{n}{d}; equivalently, there are exactly dd distinct solutions modulo nn.

Basically, ax≡c(modn)ax \equiv c \pmod n is just the statement that ax+ny=cax + ny = c for some integer yy; i.e. that cc is an integer linear combination of aa and nn. We already know from Bezouts Identity and the Extended Euclidean Algorithm that the reachable values of such combinations are precisely the multiples of d=gcd⁡(a,n)d = \gcd(a,n); so if d∤cd \nmid c, the value cc is simply unreachable (this is exactly why the 6x≡4(mod9)6x \equiv 4 \pmod 9 table never hit 44: every output was a multiple of 33).

As for the count: the solutions form one class x≡x0(modnd)x \equiv x_0 \pmod{\frac{n}{d}}, and a single class modulo nd\frac{n}{d} splits into dd separate classes modulo nn, namely

x0,x0+nd,x0+2nd,…,x0+(d−1)nd.x_0, \quad x_0 + \tfrac{n}{d}, \quad x_0 + 2\tfrac{n}{d}, \quad \dots, \quad x_0 + (d-1)\tfrac{n}{d}.

You can see this in the 6x≡4(mod8)6x \equiv 4 \pmod 8 example: d=2d = 2, the answer was one class x≡2(mod4)x \equiv 2 \pmod 4, and modulo 88 that class splits into the d=2d = 2 solutions 22 and 66.

A General Method for Solving Linear Congruences#

To solve ax≡c(modn)ax \equiv c \pmod n in general, first consider simplifying with the rules of modular arithmetic as above; if the numbers are too large for that to be practical, the following method always works:

  • Find d=gcd⁡(a,n)d = \gcd(a,n). If d∤cd \nmid c, there is no solution; stop.
  • Divide the congruence through by dd (modulus included) to get adx≡cd(modnd)\frac{a}{d}x \equiv \frac{c}{d} \pmod{\frac{n}{d}}.
  • Find the multiplicative inverse x′x' of ad\frac{a}{d} modulo nd\frac{n}{d}.
  • The general solution is x≡cdx′(modnd)x \equiv \frac{c}{d}x' \pmod{\frac{n}{d}}.

Equivalently, run the EEA directly on aa and nn to find integers x′x' and y′y' such that d=ax′+ny′d = ax' + ny'; dividing that identity by dd gives 1=adx′+ndy′1 = \frac{a}{d}x' + \frac{n}{d}y', which says precisely that this same x′x' is the inverse of ad\frac{a}{d} modulo nd\frac{n}{d}. Either way,

x≡cdx′(modnd).\boxed{x \equiv \frac{c}{d}x' \pmod{\frac{n}{d}}.}

If we want to list all dd solutions in the original modulus nn, take the solution cdx′\frac{c}{d}x' and repeatedly add nd\frac{n}{d} until there are dd different values;

x≡cdx′+ndk(modn),k∈{0,1,2,…,d−1}.x \equiv \frac{c}{d}x' + \frac{n}{d}k \pmod n, \quad k \in \{0, 1, 2, \dots, d-1\}.

Reading Everything Off the EEA Table#

Almost all the information the method needs is already sitting in the EEA table for aa and nn:

qiq_i ⋯\cdots qkq_k qk+1q_{k+1}
rir_i aa nn ⋯\cdots dd 00
xix_i 11 00 ⋯\cdots x′x' ±nd\pm\frac{n}{d}
yiy_i 00 11 ⋯\cdots y′y' ∓ad\mp\frac{a}{d}

Usually we have a<na < n, so in practice the table is seeded with nn first and the roles of the two bottom rows swap. The safe habit is to identify x′x' via the column property ri=(first seed)xi+(second seed)yir_i = (\text{first seed})x_i + (\text{second seed})y_i; x′x' is whichever entry multiplies aa, not whichever row you memorised.

Notice also why the last column is always ±nd\pm\frac{n}{d} and ∓ad\mp\frac{a}{d}: that column has ri=0r_i = 0, so its entries satisfy axi+nyi=0ax_i + ny_i = 0, and after dividing by dd the smallest integer pair achieving adxi=−ndyi\frac{a}{d}x_i = -\frac{n}{d}y_i is xi=±ndx_i = \pm\frac{n}{d}, yi=∓ady_i = \mp\frac{a}{d} (since ad\frac{a}{d} and nd\frac{n}{d} are coprime). This makes the last column a free arithmetic check on the whole table.

Example. Solve 111x≡75(mod321)111x \equiv 75 \pmod{321}.
Run the EEA on 321321 and 111111:

qiq_i 22 11 88 44
rir_i 321321 111111 9999 1212 33 00
xix_i 11 00 11 −1-1 99 −37-37
yiy_i 00 11 −2-2 33 −26-26 107107

So d=gcd⁡(111,321)=3d = \gcd(111, 321) = 3, and 3∣753 \mid 75, so solutions exist. The second-last column gives

3=321×9+111×(−26),3 = 321 \times 9 + 111 \times (-26),

and since x′x' is the coefficient of a=111a = 111, we have x′=−26x' = -26. (Quick check on the last column: −37=−1113-37 = -\frac{111}{3} and 107=3213107 = \frac{321}{3}, as promised.) Therefore,

x≡753×(−26)(mod3213)≡25×(−26)(mod107)≡−650(mod107)≡−650+7×107(mod107)≡99(mod107).\begin{align*} x &\equiv \frac{75}{3} \times (-26) \pmod{\frac{321}{3}} \\ &\equiv 25 \times (-26) \pmod{107} \\ &\equiv -650 \pmod{107} \\ &\equiv -650 + 7 \times 107 \pmod{107} \\ &\equiv 99 \pmod{107}. \end{align*}

Sanity check: 111×99=10989=34×321+75111 \times 99 = 10989 = 34 \times 321 + 75. ✓\checkmark Therefore, the general solution is x≡99(mod107)x \equiv 99 \pmod{107}; as the d=3d = 3 separate solutions modulo the original modulus, x≡99,206,313(mod321)x \equiv 99, 206, 313 \pmod{321}.

Alternatively, the rules of modular arithmetic crack this one with almost no work. Divide through by d=3d = 3 (which divides the modulus too):

111x≡75(mod321)37x≡25(mod107)111x≡75(mod107), (multiplying by 3; fine since gcd⁡(3,107)=1)4x≡75(mod107), (as 111≡4(mod107))4x≡396(mod107), (adding 3×107 to reach a multiple of 4)x≡99(mod107),\begin{align*} 111x &\equiv 75 \pmod{321} \\ 37x &\equiv 25 \pmod{107} \\ 111x &\equiv 75 \pmod{107}, \text{ (multiplying by 3; fine since } \gcd(3,107)=1) \\ 4x &\equiv 75 \pmod{107}, \text{ (as } 111 \equiv 4 \pmod{107}) \\ 4x &\equiv 396 \pmod{107}, \text{ (adding } 3 \times 107 \text{ to reach a multiple of } 4) \\ x &\equiv 99 \pmod{107}, \end{align*}

cancelling the 44 in the last step is legal because gcd⁡(4,107)=1\gcd(4, 107) = 1. Same answer; it is always worth scanning for shortcuts like this before committing to a full table.

Example. Find all solutions to 14x≡21(mod35)14x \equiv 21 \pmod{35}.
Here d=gcd⁡(14,35)=7d = \gcd(14, 35) = 7 and 7∣217 \mid 21, so solutions exist and there will be seven of them modulo 3535. Divide everything through by 77:

14x≡21(mod35)2x≡3(mod5)x≡3×3(mod5), (as 2−1≡3(mod5), since 2×3=6≡1)x≡9(mod5)x≡4(mod5).\begin{align*} 14x &\equiv 21 \pmod{35} \\ 2x &\equiv 3 \pmod 5 \\ x &\equiv 3 \times 3 \pmod 5, \text{ (as } 2^{-1} \equiv 3 \pmod 5\text{, since } 2\times 3 = 6 \equiv 1) \\ x &\equiv 9 \pmod 5 \\ x &\equiv 4 \pmod 5. \end{align*}

Sanity check: 14×4=56=35+2114 \times 4 = 56 = 35 + 21. ✓\checkmark Listing the solutions modulo the original modulus by repeatedly adding 357=5\frac{35}{7} = 5:

x≡4,9,14,19,24,29,34(mod35),x \equiv 4, 9, 14, 19, 24, 29, 34 \pmod{35},

exactly d=7d = 7 solutions, matching the existence theorem. Therefore, the general solution is x≡4(mod5)x \equiv 4 \pmod 5.

Linear Diophantine Equations#

Note

Definition
A Diophantine equation is a polynomial equation whose solutions are required to be integers. A linear Diophantine equation is an equation of the form

ax+by=c,ax + by = c,

for given integers aa, bb and cc.

Basically, over the reals ax+by=cax + by = c is just a line with infinitely many points on it; the Diophantine question is which points on that line have both coordinates integers. The integer restriction is the entire difficulty (and the entire point).

Note

Theorem (Solutions of Linear Diophantine Equations)
Let a,b,c∈Za, b, c \in \mathbb{Z} with aa and bb not both zero, and let d=gcd⁡(a,b)d = \gcd(a,b). The equation

ax+by=cax + by = c

has integer solutions if and only if d∣cd \mid c. Moreover, if (x0,y0)(x_0, y_0) is any one particular solution, then the complete set of solutions is

x=x0+bdk,y=y0−adk,k∈Z.x = x_0 + \frac{b}{d}k, \quad y = y_0 - \frac{a}{d}k, \quad k \in \mathbb{Z}.

The existence half is exactly the solvability theorem from Bezouts Identity and the Extended Euclidean Algorithm (the reachable values of ax+byax + by are precisely the multiples of dd), so we only prove the description of the solution family.

Proof. First, every member of the family really is a solution; the two shifts cancel exactly:

a(x0+bdk)+b(y0−adk)=ax0+by0+abdk−abdk=ax0+by0=c.\begin{align*} a\left(x_0 + \tfrac{b}{d}k\right) + b\left(y_0 - \tfrac{a}{d}k\right) &= ax_0 + by_0 + \tfrac{ab}{d}k - \tfrac{ab}{d}k \\ &= ax_0 + by_0 \\ &= c. \end{align*}

Conversely, no solutions are missed. Suppose (x1,y1)(x_1, y_1) is any solution; then subtracting ax0+by0=cax_0 + by_0 = c from ax1+by1=cax_1 + by_1 = c,

a(x1−x0)+b(y1−y0)=0a(x1−x0)=−b(y1−y0)ad(x1−x0)=−bd(y1−y0), (dividing by d).\begin{align*} a(x_1 - x_0) + b(y_1 - y_0) &= 0 \\ a(x_1 - x_0) &= -b(y_1 - y_0) \\ \frac{a}{d}(x_1 - x_0) &= -\frac{b}{d}(y_1 - y_0), \text{ (dividing by } d). \end{align*}

Now bd\frac{b}{d} divides the right-hand side, so it divides the left-hand side; but gcd⁡(ad,bd)=1\gcd\left(\frac{a}{d}, \frac{b}{d}\right) = 1, so bd\frac{b}{d} must divide x1−x0x_1 - x_0 itself. Writing x1−x0=bdkx_1 - x_0 = \frac{b}{d}k for some k∈Zk \in \mathbb{Z} and substituting back,

ad⋅bdk=−bd(y1−y0)y1−y0=−adk.■\begin{align*} \frac{a}{d} \cdot \frac{b}{d}k &= -\frac{b}{d}(y_1 - y_0) \\ y_1 - y_0 &= -\frac{a}{d}k. \quad \blacksquare \end{align*}

Basically, once you have found one lattice point on the line, all the others are evenly spaced along it, with the xx-coordinates stepping by bd\frac{b}{d} and the yy-coordinates stepping by ad\frac{a}{d} in the opposite direction. So a full answer to a Diophantine question always has two ingredients: a particular solution, and the step sizes.

The general method for finding the integer solutions of ax+by=cax + by = c is:

  • Consider the equation modulo bb (this eliminates the byby term, since by≡0(modb)by \equiv 0 \pmod b).
  • Solve the resulting linear congruence ax≡c(modb)ax \equiv c \pmod b.
  • Write the solution for xx in terms of an arbitrary integer parameter kk.
  • Substitute this xx back into the original equation and solve for yy in terms of kk.

Of course, we could equally consider the equation modulo aa and solve for yy first; pick whichever coefficient gives the easier congruence.

Example. Find all integer solutions to 6x+9y=46x + 9y = 4.
Here d=gcd⁡(6,9)=3d = \gcd(6,9) = 3, and 3∤43 \nmid 4; the left-hand side is always a multiple of 33 no matter which integers are substituted, and 44 is not. Therefore, there are no integer solutions. (Notice this is the same obstruction as the congruence 6x≡4(mod9)6x \equiv 4 \pmod 9 from earlier; taking the equation mod 99 produces exactly that unsolvable congruence.) Do not start hunting for particular solutions before the divisibility check passes.

Example. Find all integer solutions to 6x+7y=46x + 7y = 4.
Now gcd⁡(6,7)=1\gcd(6,7) = 1, which certainly divides 44, so solutions exist. Consider the equation modulo 77:

6x≡4(mod7),6x \equiv 4 \pmod 7,

which we solved at the start of this note; x≡3(mod7)x \equiv 3 \pmod 7, i.e. x=3+7kx = 3 + 7k for arbitrary k∈Zk \in \mathbb{Z}. Substituting back into the original equation:

6(3+7k)+7y=418+42k+7y=47y=−14−42ky=−2−6k.\begin{align*} 6(3 + 7k) + 7y &= 4 \\ 18 + 42k + 7y &= 4 \\ 7y &= -14 - 42k \\ y &= -2 - 6k. \end{align*}

Check with k=0k=0: 6×3+7×(−2)=18−14=46 \times 3 + 7 \times (-2) = 18 - 14 = 4. ✓\checkmark Therefore, the complete set of solutions is

(x,y)=(3+7k,  −2−6k),k∈Z.(x, y) = (3 + 7k, \; -2 - 6k), \quad k \in \mathbb{Z}.

(Taking the equation mod 66 instead gives 7y≡4(mod6)7y \equiv 4 \pmod 6, i.e. y≡4(mod6)y \equiv 4 \pmod 6, and substituting back produces the same family; mod out by whichever coefficient you prefer.)

Solving Linear Diophantines with the EEA#

Just as with congruences, the EEA table for aa and bb contains everything at once: the gcd dd for the divisibility check, a Bézout pair (x′,y′)(x', y') with d=ax′+by′d = ax' + by' in the second-last column, and the step sizes bd\frac{b}{d} and ad\frac{a}{d} sitting in the last column. Scaling the Bézout identity by cd\frac{c}{d} gives the particular solution, and the theorem above gives the family:

x=cdx′+bdkandy=cdy′−adk,k∈Z.\boxed{x = \frac{c}{d}x' + \frac{b}{d}k \quad \text{and} \quad y = \frac{c}{d}y' - \frac{a}{d}k, \quad k \in \mathbb{Z}.}

Example. Find all integer solutions to 6x+7y=46x + 7y = 4, using the EEA instead.
The table is short enough to do by inspection: 7−6=17 - 6 = 1, so

1=6×(−1)+7×1,1 = 6 \times (-1) + 7 \times 1,

giving d=1d = 1, x′=−1x' = -1, y′=1y' = 1. Multiplying through by cd=4\frac{c}{d} = 4:

4=6×(−4)+7×4,\begin{align*} 4 &= 6 \times (-4) + 7 \times 4, \end{align*}

so (x0,y0)=(−4,4)(x_0, y_0) = (-4, 4) is a particular solution, and the steps are bd=7\frac{b}{d} = 7 and ad=6\frac{a}{d} = 6. Therefore,

(x,y)=(−4+7k,  4−6k),k∈Z.(x, y) = (-4 + 7k, \; 4 - 6k), \quad k \in \mathbb{Z}.

This looks different from the answer in the previous section, but it is the same set; two correct general solutions can look completely different, differing only by a shift of the parameter. Replacing kk by k+1k+1 here gives (3+7k,−2−6k)(3 + 7k, -2 - 6k), exactly the earlier family. To confirm two families agree, check that they share a particular solution and have the same step sizes.

Example. Solve 533x+182y=481533x + 182y = 481 over the integers.
Run the EEA on 533533 and 182182:

qiq_i 22 11 1313
rir_i 533533 182182 169169 1313 00
xix_i 11 00 11 −1-1 1414
yiy_i 00 11 −2-2 33 −41-41

So d=gcd⁡(533,182)=13d = \gcd(533, 182) = 13, and 481=13×37481 = 13 \times 37, so solutions exist with cd=37\frac{c}{d} = 37. The second-last column gives

13=533×(−1)+182×3,13 = 533 \times (-1) + 182 \times 3,

i.e. x′=−1x' = -1 and y′=3y' = 3, and the last column confirms the steps bd=18213=14\frac{b}{d} = \frac{182}{13} = 14 and ad=53313=41\frac{a}{d} = \frac{533}{13} = 41. Scaling by 3737:

x0=37×(−1)=−37,y0=37×3=111.\begin{align*} x_0 &= 37 \times (-1) \\ &= -37, \\ y_0 &= 37 \times 3 \\ &= 111. \end{align*}

Check: 533×(−37)+182×111=−19721+20202=481533 \times (-37) + 182 \times 111 = -19721 + 20202 = 481. ✓\checkmark Therefore, the complete set of solutions is

(x,y)=(−37+14k,  111−41k),k∈Z.(x, y) = (-37 + 14k, \; 111 - 41k), \quad k \in \mathbb{Z}.

Alternatively, by the congruence method: taking the equation modulo 182182,

533x≡481(mod182)169x≡117(mod182), (reducing both numbers mod 182)13x≡9(mod14), (dividing by 13=gcd⁡(169,182), modulus included)−x≡9(mod14), (as 13≡−1(mod14))x≡−9(mod14)x≡5(mod14),\begin{align*} 533x &\equiv 481 \pmod{182} \\ 169x &\equiv 117 \pmod{182}, \text{ (reducing both numbers mod } 182) \\ 13x &\equiv 9 \pmod{14}, \text{ (dividing by } 13 = \gcd(169,182), \text{ modulus included}) \\ -x &\equiv 9 \pmod{14}, \text{ (as } 13 \equiv -1 \pmod{14}) \\ x &\equiv -9 \pmod{14} \\ x &\equiv 5 \pmod{14}, \end{align*}

so x=5+14kx = 5 + 14k. Substituting back:

533(5+14k)+182y=4812665+7462k+182y=481182y=−2184−7462ky=−12−41k.\begin{align*} 533(5 + 14k) + 182y &= 481 \\ 2665 + 7462k + 182y &= 481 \\ 182y &= -2184 - 7462k \\ y &= -12 - 41k. \end{align*}

Therefore, (x,y)=(5+14k,  −12−41k)(x,y) = (5 + 14k, \; -12 - 41k); the same set as before, since replacing kk by k+3k + 3 in the first family gives exactly this one.

One Congruence, Two Ways#

It is important to note that linear congruences and linear Diophantine equations are the same problem wearing different clothes; ax≡c(modn)ax \equiv c \pmod n says precisely that ax+ny=cax + ny = c for some integer yy. So any congruence can be attacked as an equation, and vice versa, and the answers must agree.

Example. Solve 5x≡3(mod11)5x \equiv 3 \pmod{11} (a) using a multiplicative inverse, and (b) as a Diophantine equation, and confirm the answers match.
(a) Since gcd⁡(5,11)=1\gcd(5,11) = 1, the inverse of 55 exists; by inspection 5×9=45=44+1≡1(mod11)5 \times 9 = 45 = 44 + 1 \equiv 1 \pmod{11}, so 5−1≡95^{-1} \equiv 9. Multiplying both sides by 99:

x≡9×3(mod11)≡27(mod11)≡5(mod11).\begin{align*} x &\equiv 9 \times 3 \pmod{11} \\ &\equiv 27 \pmod{11} \\ &\equiv 5 \pmod{11}. \end{align*}

(b) Rewrite the congruence as the equation 5x+11y=35x + 11y = 3. By inspection (or a two-line EEA), 5×(−2)+11×1=15 \times (-2) + 11 \times 1 = 1; multiplying through by 33 gives the particular solution (x0,y0)=(−6,3)(x_0, y_0) = (-6, 3), and the steps are bd=11\frac{b}{d} = 11, ad=5\frac{a}{d} = 5. So

(x,y)=(−6+11k,  3−5k),k∈Z.(x, y) = (-6 + 11k, \; 3 - 5k), \quad k \in \mathbb{Z}.

The xx-values −6+11k-6 + 11k are exactly the congruence class x≡5(mod11)x \equiv 5 \pmod{11} (take k=1k = 1), matching part (a). ✓\checkmark Therefore, both methods give x≡5(mod11)x \equiv 5 \pmod{11}.

The decision between them: if the question only asks for xx, the congruence machinery is leaner (you never compute or carry yy); if the question is stated as an equation, or you genuinely need both unknowns, work with the Diophantine form. Either way the underlying computation is the same EEA table, so choose whichever bookkeeping you find harder to fumble.

Restrictions on Solutions#

Linear Diophantine equations often arise in contexts where the otherwise infinite solution set is restricted further; by far the most common restriction is that only positive (or non-negative) solutions make sense. The strategy is not to change the method at all: solve normally to get the complete family in terms of kk, then impose the restrictions, which turn into inequalities on kk; typically only finitely many integers kk survive.

Example. Find all positive integer solutions to 2x+5y=172x + 5y = 17.
Since gcd⁡(2,5)=1∣17\gcd(2,5) = 1 \mid 17, solutions exist. Take the equation modulo 22:

5y≡17(mod2)y≡1(mod2), (as 5≡1 and 17≡1(mod2)),\begin{align*} 5y &\equiv 17 \pmod 2 \\ y &\equiv 1 \pmod 2, \text{ (as } 5 \equiv 1 \text{ and } 17 \equiv 1 \pmod 2), \end{align*}

so y=1+2ky = 1 + 2k. Substituting back:

2x+5(1+2k)=172x=12−10kx=6−5k.\begin{align*} 2x + 5(1 + 2k) &= 17 \\ 2x &= 12 - 10k \\ x &= 6 - 5k. \end{align*}

The complete integer family is (x,y)=(6−5k,  1+2k)(x,y) = (6 - 5k, \; 1 + 2k). Now impose positivity;

x≥1  ⟹  6−5k≥1  ⟹  k≤1,y≥1  ⟹  1+2k≥1  ⟹  k≥0,\begin{align*} x \geq 1 &\implies 6 - 5k \geq 1 \implies k \leq 1, \\ y \geq 1 &\implies 1 + 2k \geq 1 \implies k \geq 0, \end{align*}

so k∈{0,1}k \in \{0, 1\}, giving (x,y)=(6,1)(x,y) = (6,1) and (1,3)(1,3). Check: 2×6+5×1=172 \times 6 + 5 \times 1 = 17 and 2×1+5×3=172 \times 1 + 5 \times 3 = 17. ✓\checkmark Therefore, there are exactly two positive solutions, (6,1)(6, 1) and (1,3)(1, 3).

Example. A parcel requires exactly $1.35\$1.35 of postage, and you only have 2020c stamps and 2525c stamps. In how many ways can you make up the postage?
Working in cents, we need non-negative integer solutions to 20x+25y=13520x + 25y = 135. First, gcd⁡(20,25)=5\gcd(20, 25) = 5 and 5∣1355 \mid 135, so solutions exist; divide the whole equation by 55 to keep the numbers small:

4x+5y=27.4x + 5y = 27.

Take this modulo 44:

5y≡27(mod4)y≡3(mod4), (as 5≡1 and 27≡3(mod4)),\begin{align*} 5y &\equiv 27 \pmod 4 \\ y &\equiv 3 \pmod 4, \text{ (as } 5 \equiv 1 \text{ and } 27 \equiv 3 \pmod 4), \end{align*}

so y=3+4ky = 3 + 4k. Substituting back:

4x+5(3+4k)=274x=12−20kx=3−5k.\begin{align*} 4x + 5(3 + 4k) &= 27 \\ 4x &= 12 - 20k \\ x &= 3 - 5k. \end{align*}

Both stamp counts must be non-negative;

x≥0  ⟹  3−5k≥0  ⟹  k≤0,y≥0  ⟹  3+4k≥0  ⟹  k≥0,\begin{align*} x \geq 0 &\implies 3 - 5k \geq 0 \implies k \leq 0, \\ y \geq 0 &\implies 3 + 4k \geq 0 \implies k \geq 0, \end{align*}

which forces k=0k = 0, i.e. (x,y)=(3,3)(x, y) = (3, 3). Check: 3×20+3×25=60+75=1353 \times 20 + 3 \times 25 = 60 + 75 = 135 cents. ✓\checkmark Therefore, there is exactly one way: three 2020c stamps and three 2525c stamps.

Example. How many positive integer solutions does 7x+11y=3007x + 11y = 300 have?
Since gcd⁡(7,11)=1∣300\gcd(7,11) = 1 \mid 300, solutions exist. Take the equation modulo 77:

11y≡300(mod7)4y≡6(mod7), (as 11≡4 and 300=42×7+6)y≡2×6(mod7), (as 4−1≡2(mod7), since 4×2=8≡1)y≡12(mod7)y≡5(mod7),\begin{align*} 11y &\equiv 300 \pmod 7 \\ 4y &\equiv 6 \pmod 7, \text{ (as } 11 \equiv 4 \text{ and } 300 = 42 \times 7 + 6) \\ y &\equiv 2 \times 6 \pmod 7, \text{ (as } 4^{-1} \equiv 2 \pmod 7\text{, since } 4 \times 2 = 8 \equiv 1) \\ y &\equiv 12 \pmod 7 \\ y &\equiv 5 \pmod 7, \end{align*}

so y=5+7ky = 5 + 7k. Substituting back:

7x+11(5+7k)=3007x=245−77kx=35−11k.\begin{align*} 7x + 11(5 + 7k) &= 300 \\ 7x &= 245 - 77k \\ x &= 35 - 11k. \end{align*}

Imposing positivity;

x≥1  ⟹  35−11k≥1  ⟹  k≤3411  ⟹  k≤3,y≥1  ⟹  5+7k≥1  ⟹  k≥−47  ⟹  k≥0,\begin{align*} x \geq 1 &\implies 35 - 11k \geq 1 \implies k \leq \tfrac{34}{11} \implies k \leq 3, \\ y \geq 1 &\implies 5 + 7k \geq 1 \implies k \geq -\tfrac{4}{7} \implies k \geq 0, \end{align*}

so k∈{0,1,2,3}k \in \{0, 1, 2, 3\}, giving the solutions

(x,y)=(35,5),  (24,12),  (13,19),  (2,26).(x, y) = (35, 5), \; (24, 12), \; (13, 19), \; (2, 26).

Check one: 7×24+11×12=168+132=3007 \times 24 + 11 \times 12 = 168 + 132 = 300. ✓\checkmark Therefore, there are exactly 44 positive integer solutions.

Notice how in all of these, the restriction step is pure bookkeeping; once the family is parametrised, convert every condition into a bound on kk, intersect the bounds, and count the surviving integers. When rounding the bounds on kk, remember that k≤3411k \leq \frac{34}{11} means k≤3k \leq 3 (round the upper bound down) and k≥−47k \geq -\frac{4}{7} means k≥0k \geq 0 (round the lower bound up); rounding these the wrong way quietly adds or deletes a solution.