Math Core

Module 3.2 · Number Theory

The Chinese remainder theorem

The Chinese remainder theorem (CRT) says that knowing a number mod 88 and mod 125125 is the same as knowing it mod 10001000. That turns one hard modulus into several easy ones. On the AIME, where every answer is a remainder mod 10001000 in disguise, this is one of the most-used tools you have.

The theorem

Chinese remainder theorem

If m1,m2,…,mkm_1, m_2, \dots, m_k are pairwise relatively prime and M=m1m2⋯mkM = m_1 m_2 \cdots m_k, then for any remainders r1,…,rkr_1, \dots, r_k the system

x≡r1(modm1),x≡r2(modm2),…,x≡rk(modmk)x \equiv r_1 \pmod{m_1}, \quad x \equiv r_2 \pmod{m_2}, \quad \dots, \quad x \equiv r_k \pmod{m_k}

has a solution, and the solution is unique mod MM.

Why it's true (two moduli). Take m,nm, n with gcd⁡(m,n)=1\gcd(m, n) = 1 and look at the mnmn numbers 0,1,…,mn−10, 1, \dots, mn - 1. Send each xx to its pair of remainders (x mod m, x mod n)(x \bmod m, \ x \bmod n).

  • Different xx give different pairs. If xx and yy give the same pair, then m∣x−ym \mid x - y and n∣x−yn \mid x - y, so mn∣x−ymn \mid x - y (because m,nm, n are coprime). Since ∣x−y∣<mn|x - y| < mn, x=yx = y.
  • Every pair is hit. There are mnmn inputs, all giving different pairs, and exactly mnmn possible pairs.

So each pair of remainders corresponds to exactly one residue mod mnmn. For more moduli, apply this repeatedly.

A constructive version. By Bézout, there are integers u,vu, v with um+vn=1um + vn = 1. Then x=r2um+r1vnx = r_2 um + r_1 vn works: mod mm it is r1vn≡r1r_1 vn \equiv r_1 (since vn≡1vn \equiv 1), and mod nn it is r2um≡r2r_2 um \equiv r_2.

Solving systems in practice

On a contest you rarely need Bézout. Two faster methods:

  1. Sieve. List numbers satisfying the congruence with the largest modulus, and test them against the next one. Once you find a match, step by the product of the moduli so far.
  2. Substitute. From x≡r1(modm1)x \equiv r_1 \pmod{m_1} write x=r1+m1tx = r_1 + m_1 t, plug into the next congruence, and solve for tt.

Common mistake

CRT needs pairwise coprime moduli. If they share a factor, the system might have no solution: x≡1(mod6)x \equiv 1 \pmod 6 and x≡2(mod4)x \equiv 2 \pmod 4 is impossible, since the first says xx is odd and the second says xx is even. When the moduli share g=gcd⁡(m,n)g = \gcd(m, n), a solution exists exactly when r1≡r2(modg)r_1 \equiv r_2 \pmod g, and then it is unique mod lcm⁡(m,n)\operatorname{lcm}(m, n), not mod mnmn.

Counting with CRT

CRT also counts. If ff is a polynomial with integer coefficients, then f(x)≡0(modmn)f(x) \equiv 0 \pmod{mn} exactly when f(x)≡0(modm)f(x) \equiv 0 \pmod m and f(x)≡0(modn)f(x) \equiv 0 \pmod n. So

#{solutions mod mn}=#{solutions mod m}×#{solutions mod n}.\#\{\text{solutions mod } mn\} = \#\{\text{solutions mod } m\} \times \#\{\text{solutions mod } n\}.

For example, x2≡1(mod1000)x^2 \equiv 1 \pmod{1000} has 44 solutions mod 88 (every odd xx) and 22 mod 125125 (x≡±1x \equiv \pm 1), so 88 solutions mod 10001000.

Worked examples

Worked example: A basic system

Find the smallest positive xx with x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5, x≡2(mod7)x \equiv 2 \pmod 7.

Numbers ≡2(mod7)\equiv 2 \pmod 7: 2,9,16,23,30,…2, 9, 16, 23, 30, \dots The first that is ≡3(mod5)\equiv 3 \pmod 5 is 2323. So x≡23(mod35)x \equiv 23 \pmod{35}. Check mod 33: 23≡223 \equiv 2. It already works, so x=23x = 23.

Worked example: Splitting 1000

Find the last three digits of 21002^{100}.

Mod 88: 2100≡02^{100} \equiv 0. Mod 125125: by Euler, 2100≡12^{100} \equiv 1 since φ(125)=100\varphi(125) = 100.

Now find x≡1(mod125)x \equiv 1 \pmod{125} with 8∣x8 \mid x: try 1,126,251,3761, 126, 251, 376. Only 376376 is divisible by 88. The last three digits are 376376.

Worked example: Consecutive numbers

Find the smallest positive nn such that 4∣n4 \mid n, 9∣n+19 \mid n + 1 and 25∣n+225 \mid n + 2.

The conditions are n≡0(mod4)n \equiv 0 \pmod 4, n≡8(mod9)n \equiv 8 \pmod 9, n≡23(mod25)n \equiv 23 \pmod{25}.

Numbers ≡23(mod25)\equiv 23 \pmod{25}: 23,48,73,98,…23, 48, 73, 98, \dots Those also ≡0(mod4)\equiv 0 \pmod 4: 4848 is the first, so n≡48(mod100)n \equiv 48 \pmod{100}. Now test 48,148,248,…48, 148, 248, \dots mod 99 (digit sums 12,13,14,…12, 13, 14, \dots so remainders 3,4,5,6,7,83, 4, 5, 6, 7, 8): the sixth one, 548548, works.

Check: 548=4⋅137548 = 4 \cdot 137, 549=9⋅61549 = 9 \cdot 61, 550=25⋅22550 = 25 \cdot 22. So n=548n = 548.

Worked example: Moduli that aren't coprime

Solve x≡5(mod12)x \equiv 5 \pmod{12} and x≡11(mod18)x \equiv 11 \pmod{18}.

Here gcd⁡(12,18)=6\gcd(12, 18) = 6. The system is consistent because 5≡11(mod6)5 \equiv 11 \pmod 6. The solution is unique mod lcm⁡(12,18)=36\operatorname{lcm}(12, 18) = 36. Numbers ≡5(mod12)\equiv 5 \pmod{12} below 3636: 5,17,295, 17, 29. Only 2929 is ≡11(mod18)\equiv 11 \pmod{18}. So x≡29(mod36)x \equiv 29 \pmod{36}.

Tip

When a remainder condition looks like "n+1n + 1 is divisible by 2,3,4,5,62, 3, 4, 5, 6", rewrite it as n≡−1n \equiv -1 modulo the lcm. Negative remainders often make systems collapse into one congruence.

Practice

Practice 1

Find the smallest positive integer that leaves remainder 22 when divided by 55, remainder 33 when divided by 77, and remainder 44 when divided by 99.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

How many integers nn with 1≤n≤10001 \le n \le 1000 satisfy n≡1(mod4)n \equiv 1 \pmod 4, n≡2(mod5)n \equiv 2 \pmod 5 and n≡3(mod7)n \equiv 3 \pmod 7?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

Find the remainder when 220262^{2026} is divided by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

A treasure chest holds fewer than 10001000 coins. When the coins are split evenly among 77 pirates, 33 are left over; among 1111 pirates, 55 are left over; among 1313 pirates, 44 are left over. How many coins are in the chest?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

How many positive integers n≤999n \le 999 satisfy both n≡3(mod6)n \equiv 3 \pmod 6 and n≡7(mod10)n \equiv 7 \pmod{10}?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

How many integers xx with 0≤x≤9990 \le x \le 999 satisfy x3≡x(mod1000)x^3 \equiv x \pmod{1000}?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Find the smallest positive integer nn such that n2+nn^2 + n is divisible by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

Find the remainder when the product 1⋅3⋅5⋅7⋯9991 \cdot 3 \cdot 5 \cdot 7 \cdots 999 of all odd numbers from 11 to 999999 is divided by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice