A linear congruence $ax \equiv b \pmod{n}$ is the mod-$n$ version of $ax = b$. It is really a linear Diophantine equation in disguise, $ax - ny = b$, so we already know when it can be solved. With inverses we can solve it quickly and count its solutions.
Theorem. Let $d = \gcd(a, n)$. The congruence $ax \equiv b \pmod{n}$ has a solution exactly when $d \mid b$. In that case it has exactly $d$ solutions mod $n$: if $x_0$ solves $$\frac{a}{d}\,x \equiv \frac{b}{d} \pmod{\frac{n}{d}},$$ then the solutions mod $n$ are $x_0,\ x_0 + \frac{n}{d},\ x_0 + 2\frac{n}{d},\ \dots,\ x_0 + (d - 1)\frac{n}{d}$.
Why it works
The picture behind the count: the map $x \mapsto ax \bmod n$ hits only the multiples of $d$, and it hits each of them exactly $d$ times, because $ax \equiv ax' \pmod{n}$ exactly when $x \equiv x' \pmod{n/d}$. So a right side $b$ is hit $d$ times or not at all.
Worked example: One solution
- $\gcd(7, 12) = 1$, so there is exactly one solution mod 12.
- $7^{-1} \equiv 7 \pmod{12}$, since $49 = 4 \cdot 12 + 1$.
- $x \equiv 7 \cdot 3 = 21 \equiv 9$. Check: $7 \cdot 9 = 63 = 5 \cdot 12 + 3$.
Answer: $x \equiv 9 \pmod{12}$.
Worked example: Several solutions
- $d = \gcd(10, 14) = 2$ and $2 \mid 6$, so there are two solutions mod 14.
- Divide $a$, $b$ and $n$ by 2: $5x \equiv 3 \pmod{7}$.
- $5^{-1} \equiv 3 \pmod{7}$ (since $15 \equiv 1$), so $x \equiv 9 \equiv 2 \pmod{7}$.
- Mod 14: $x \equiv 2$ or $2 + 7 = 9$. Check: $10 \cdot 9 = 90 = 6 \cdot 14 + 6$.
Answer: $x \equiv 2$ or $9 \pmod{14}$.
Worked example: No solution
- $\gcd(6, 15) = 3$, and $3 \nmid 7$.
- $6x - 15y$ is always a multiple of 3, so it is never 7.
Answer: No solution.
Common mistake
Key idea