MathComps LogoMathComps
ProblemsHandoutsGuideNews
Sign in

Inverses Modulo ppp

Author
Patrik Bak
Download (PDF)Download problems (PDF)

1Introduction

We assume familiarity with congruences modulo a prime ppp. In this handout we build a small theory of multiplicative inverses modulo ppp and use it to solve problems.

2Theory

Throughout, ppp denotes a prime.

2.1Recap

We freely use the following standard facts about congruences modulo ppp:

  • Congruences may be added, subtracted, and multiplied: if a≡ba \equiv ba≡b and c≡d(modp)c \equiv d \pmod{p}c≡d(modp), then a±c≡b±da \pm c \equiv b \pm da±c≡b±d and ac≡bd(modp)ac \equiv bd \pmod{p}ac≡bd(modp).
  • Cancellation: if a≢0(modp)a \not\equiv 0 \pmod{p}a≡0(modp) and ax≡ay(modp)ax \equiv ay \pmod{p}ax≡ay(modp), then x≡y(modp)x \equiv y \pmod{p}x≡y(modp).
  • For any a≢0(modp)a \not\equiv 0 \pmod{p}a≡0(modp), the residues a,2a,…,(p−1)aa, 2a, \ldots, (p-1)aa,2a,…,(p−1)a are a permutation of 1,2,…,p−11, 2, \ldots, p-11,2,…,p−1 modulo ppp.
  • Fermat's Little Theorem: for any a≢0(modp)a \not\equiv 0 \pmod{p}a≡0(modp), we have ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}ap−1≡1(modp).
  • Order: the order of a≢0(modp)a \not\equiv 0 \pmod{p}a≡0(modp) is the smallest positive integer ddd with ad≡1(modp)a^d \equiv 1 \pmod{p}ad≡1(modp). It divides p−1p - 1p−1.
  • Primitive root: there exists g≢0(modp)g \not\equiv 0 \pmod{p}g≡0(modp) of order p−1p - 1p−1. Every a≢0(modp)a \not\equiv 0 \pmod{p}a≡0(modp) equals gkg^kgk for some kkk.

2.2Inverses

Theorem 1

Existence and Uniqueness of Inverse

Let a≢0(modp)a \not\equiv 0 \pmod{p}a≡0(modp). Then there exists a unique residue a−1(modp)a^{-1} \pmod{p}a−1(modp) with

a⋅a−1≡1(modp).a \cdot a^{-1} \equiv 1 \pmod{p}.a⋅a−1≡1(modp).

We write 1a\frac{1}{a}a1​ for a−1a^{-1}a−1, and ba\frac{b}{a}ab​ for b⋅a−1b \cdot a^{-1}b⋅a−1.

Theorem 2

Inverses Add Like Fractions

Let b,d≢0(modp)b, d \not\equiv 0 \pmod{p}b,d≡0(modp). Then for any a,ca, ca,c, we have

ab+cd≡a⋅b−1+c⋅d−1≡(ad+bc)⋅(bd)−1≡ad+bcbd(modp),\frac{a}{b} + \frac{c}{d} \equiv a \cdot b^{-1} + c \cdot d^{-1} \equiv (ad + bc) \cdot (bd)^{-1} \equiv \frac{ad + bc}{bd} \pmod{p},ba​+dc​≡a⋅b−1+c⋅d−1≡(ad+bc)⋅(bd)−1≡bdad+bc​(modp),

just like normal fractions.

Theorem 3

Inverses Multiply Like Fractions

Let b,d≢0(modp)b, d \not\equiv 0 \pmod{p}b,d≡0(modp). Then for any a,ca, ca,c, we have

ab⋅cd≡(a⋅b−1)⋅(c⋅d−1)≡(ac)⋅(bd)−1≡acbd(modp),\frac{a}{b} \cdot \frac{c}{d} \equiv (a \cdot b^{-1}) \cdot (c \cdot d^{-1}) \equiv (ac) \cdot (bd)^{-1} \equiv \frac{ac}{bd} \pmod{p},ba​⋅dc​≡(a⋅b−1)⋅(c⋅d−1)≡(ac)⋅(bd)−1≡bdac​(modp),

just like normal fractions.

3Problems

The problems below all benefit from the theory above — working with fractions modulo ppp as if they were ordinary numbers. They are ordered roughly by difficulty.

Problem 1

Wilson

Let ppp be a prime. Prove that

(p−1)!≡−1(modp).(p-1)! \equiv -1 \pmod{p}.(p−1)!≡−1(modp).
1Hint

In the product 1⋅2⋯(p−1)1 \cdot 2 \cdots (p-1)1⋅2⋯(p−1), try to pair factors with their inverse. Which numbers do not have a pair?

2Hint

The numbers without the pair are those satisfying k≡1/kmod  pk \equiv 1/k \mod pk≡1/kmodp. Find them all.

Problem 2

Let ppp be a prime. Prove that there exist integers a,ba, ba,b, neither divisible by ppp, with p∣a2+b2p \mid a^2 + b^2p∣a2+b2 if and only if p=2p = 2p=2 or p≡1(mod4)p \equiv 1 \pmod{4}p≡1(mod4).

1Hint

The condition p∣a2+b2p \mid a^2 + b^2p∣a2+b2 rewrites as (a/b)2≡−1(modp)(a/b)^2 \equiv -1 \pmod{p}(a/b)2≡−1(modp). So the question becomes: for which primes is −1-1−1 a square modulo ppp?

2Hint

For one direction, raise the relation (a/b)2≡−1(a/b)^2 \equiv -1(a/b)2≡−1 to the (p−1)/2(p-1)/2(p−1)/2-th power. The left side becomes (a/b)p−1(a/b)^{p-1}(a/b)p−1.

3Hint

Apply Fermat's Little Theorem to the left side. What does the resulting equation force on pmod  4p \mod 4pmod4?

4Hint

For the converse with p≡1(mod4)p \equiv 1 \pmod{4}p≡1(mod4), you need an explicit square root of −1-1−1 modulo ppp. A natural source: Wilson's theorem, (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod{p}(p−1)!≡−1(modp).

5Hint

In the product (p−1)!(p-1)!(p−1)!, pair kkk with p−k≡−kp - k \equiv -kp−k≡−k. What does the result look like for p≡1(mod4)p \equiv 1 \pmod{4}p≡1(mod4)?

Problem 3

Let p≡2(mod3)p \equiv 2 \pmod{3}p≡2(mod3) be a prime. Show that if p∣a2+ab+b2p \mid a^2 + ab + b^2p∣a2+ab+b2, then p∣ap \mid ap∣a and p∣bp \mid bp∣b.

1Hint

Note that a3−b3=(a−b)(a2+ab+b2)a^3 - b^3 = (a - b)(a^2 + ab + b^2)a3−b3=(a−b)(a2+ab+b2), so p∣a3−b3p \mid a^3 - b^3p∣a3−b3. Assume for contradiction that p∤bp \nmid bp∤b. This is similar to the previous problem.

2Hint

Raise to the (p−2)/3(p-2)/3(p−2)/3-th power, which is a positive integer since p≡2(mod3)p \equiv 2 \pmod{3}p≡2(mod3). The left side becomes (a/b)p−2(a/b)^{p-2}(a/b)p−2.

3Hint

Multiply both sides by a/ba/ba/b to get (a/b)p−1≡a/b(modp)(a/b)^{p-1} \equiv a/b \pmod{p}(a/b)p−1≡a/b(modp). Apply Fermat's Little Theorem to conclude a≡b(modp)a \equiv b \pmod{p}a≡b(modp). Substitute this back.

Problem 4

Let ppp be a prime and let 0≤k≤p−10 \leq k \leq p-10≤k≤p−1 be an integer. Prove that

(p−1k)≡(−1)k(modp).\binom{p-1}{k} \equiv (-1)^k \pmod{p}.(kp−1​)≡(−1)k(modp).
1Hint

Expand the binomial coefficient as (p−1k)=(p−1)(p−2)⋯(p−k)k!\binom{p-1}{k} = \frac{(p-1)(p-2) \cdots (p-k)}{k!}(kp−1​)=k!(p−1)(p−2)⋯(p−k)​.

2Hint

Reduce each factor in the numerator modulo ppp: p−j≡−j(modp)p - j \equiv -j \pmod{p}p−j≡−j(modp). Aren't we left with something familiar?

Problem 5

Let p≥3p \geq 3p≥3 be a prime. Show that if

11+12+13+⋯+1p−1=mn,\frac{1}{1} + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{p-1} = \frac{m}{n},11​+21​+31​+⋯+p−11​=nm​,

then p∣mp \mid mp∣m.

1Hint

Looking at this modulo ppp, we have the sum of inverses of all possible non-zero values mod ppp.

2Hint

Realize that this sum is actually the sum of all values from 111 to p−1p-1p−1, not necessarily in this order.

Problem 6

IMO 2005

Determine all positive integers nnn that are coprime to every term of the sequence

am=2m+3m+6m−1,m≥1.a_m = 2^m + 3^m + 6^m - 1, \quad m \geq 1.am​=2m+3m+6m−1,m≥1.
1Hint

Show that for every prime ppp, some ama_mam​ is divisible by ppp. Then the only valid nnn is n=1n = 1n=1.

2Hint

Small primes are easy: a1=10a_1 = 10a1​=10 handles p=2,5p = 2, 5p=2,5 and a2=48a_2 = 48a2​=48 handles p=3p = 3p=3. Focus on p≥5p \geq 5p≥5 and try to choose mmm depending on ppp so that the four terms collapse.

3Hint

For p≥5p \geq 5p≥5, take m=p−2m = p - 2m=p−2. Then 2m≡1/22^m \equiv 1/22m≡1/2, 3m≡1/33^m \equiv 1/33m≡1/3, 6m≡1/6(modp)6^m \equiv 1/6 \pmod{p}6m≡1/6(modp). Compute ap−2(modp)a_{p-2} \pmod{p}ap−2​(modp).

Problem 7

Poland

Let p>3p > 3p>3 be a prime with p≡2(mod3)p \equiv 2 \pmod{3}p≡2(mod3), and let ak=k2+k+1a_k = k^2 + k + 1ak​=k2+k+1 for k=1,2,…,p−1k = 1, 2, \ldots, p-1k=1,2,…,p−1. Prove that

a1a2⋯ap−1≡3(modp).a_1 a_2 \cdots a_{p-1} \equiv 3 \pmod{p}.a1​a2​⋯ap−1​≡3(modp).
1Hint

For k≠1k \neq 1k=1, factor k2+k+1=k3−1k−1k^2 + k + 1 = \frac{k^3 - 1}{k - 1}k2+k+1=k−1k3−1​. Isolate the k=1k = 1k=1 term, which contributes 333.

2Hint

The product becomes 3⋅∏k=2p−1(k3−1)(p−2)!3 \cdot \frac{\prod_{k=2}^{p-1}(k^3 - 1)}{(p-2)!}3⋅(p−2)!∏k=2p−1​(k3−1)​. We need to calculate the numerator and denominator mod ppp separately.

3Hint

It would be great if the product of all the numbers k3−1k^3-1k3−1 was actually something we could calculate.

Problem 8

Singapore 2012

Let ppp be an odd prime. Prove that

∑i=1(p−1)/2ip−2≡2−2pp(modp).\sum_{i=1}^{(p-1)/2} i^{p-2} \equiv \frac{2 - 2^p}{p} \pmod{p}.i=1∑(p−1)/2​ip−2≡p2−2p​(modp).
1Hint

Since ip−2≡1/i(modp)i^{p-2} \equiv 1/i \pmod{p}ip−2≡1/i(modp), the left-hand side is ∑i=1(p−1)/21/i\sum_{i=1}^{(p-1)/2} 1/i∑i=1(p−1)/2​1/i. Now work on the right-hand side to get the hint.

2Hint

Expand 2p=(1+1)p=∑k=0p(pk)2^p = (1+1)^p = \sum_{k=0}^{p} \binom{p}{k}2p=(1+1)p=∑k=0p​(kp​), so 2p−2p=1p∑k=1p−1(pk)\frac{2^p - 2}{p} = \frac{1}{p} \sum_{k=1}^{p-1} \binom{p}{k}p2p−2​=p1​∑k=1p−1​(kp​).

3Hint

Use (pk)=pk(p−1k−1)\binom{p}{k} = \frac{p}{k} \binom{p-1}{k-1}(kp​)=kp​(k−1p−1​) to rewrite each summand: 2p−2p=∑k=1p−1(p−1k−1)k\frac{2^p - 2}{p} = \sum_{k=1}^{p-1} \frac{\binom{p-1}{k-1}}{k}p2p−2​=∑k=1p−1​k(k−1p−1​)​.

4Hint

Apply (p−1k−1)≡(−1)k−1(modp)\binom{p-1}{k-1} \equiv (-1)^{k-1} \pmod{p}(k−1p−1​)≡(−1)k−1(modp) from a previous problem to simplify.

5Hint

The right-hand side reduces to −∑k=1p−1(−1)k−1/k(modp)-\sum_{k=1}^{p-1} (-1)^{k-1}/k \pmod{p}−∑k=1p−1​(−1)k−1/k(modp). Split this into even and odd kkk, and use ∑k=1p−11/k≡0(modp)\sum_{k=1}^{p-1} 1/k \equiv 0 \pmod{p}∑k=1p−1​1/k≡0(modp) from a previous problem.

Problem 9

Show that for every prime ppp,

(2pp)≡2(modp2).\binom{2p}{p} \equiv 2 \pmod{p^2}.(p2p​)≡2(modp2).
1Hint

Write (2pp)=(2p)(2p−1)⋯(p+1)p!\binom{2p}{p} = \frac{(2p)(2p-1)\cdots(p+1)}{p!}(p2p​)=p!(2p)(2p−1)⋯(p+1)​. Pull out the factor 2p2p2p in the numerator and the ppp in p!=p⋅(p−1)!p! = p \cdot (p-1)!p!=p⋅(p−1)! to get (2pp)=2∏k=1p−1(p+k)(p−1)!\binom{2p}{p} = \frac{2 \prod_{k=1}^{p-1}(p+k)}{(p-1)!}(p2p​)=(p−1)!2∏k=1p−1​(p+k)​.

2Hint

Factor each numerator term as p+k=k(1+pk)p + k = k\bigl(1 + \frac{p}{k}\bigr)p+k=k(1+kp​), so ∏k=1p−1(p+k)=(p−1)!⋅∏k=1p−1(1+pk)\prod_{k=1}^{p-1}(p+k) = (p-1)! \cdot \prod_{k=1}^{p-1}\bigl(1 + \frac{p}{k}\bigr)∏k=1p−1​(p+k)=(p−1)!⋅∏k=1p−1​(1+kp​).

3Hint

Expand ∏(1+pk)\prod\bigl(1 + \frac{p}{k}\bigr)∏(1+kp​) modulo p2p^2p2: only the constant and linear-in-ppp terms survive, giving 1+p∑k=1p−11/k1 + p \sum_{k=1}^{p-1} 1/k1+p∑k=1p−1​1/k.

4Hint

By P4, ∑k=1p−11/k≡0(modp)\sum_{k=1}^{p-1} 1/k \equiv 0 \pmod{p}∑k=1p−1​1/k≡0(modp), so p∑1/k≡0(modp2)p \sum 1/k \equiv 0 \pmod{p^2}p∑1/k≡0(modp2).

Problem 10

Let p≥5p \geq 5p≥5 be a prime and let eee be a positive integer with (p−1)∤e(p-1) \nmid e(p−1)∤e. Show that if

11e+12e+13e+⋯+1(p−1)e=mn,\frac{1}{1^e} + \frac{1}{2^e} + \frac{1}{3^e} + \cdots + \frac{1}{(p-1)^e} = \frac{m}{n},1e1​+2e1​+3e1​+⋯+(p−1)e1​=nm​,

then p∣mp \mid mp∣m.

1Hint

The claim is ∑k=1p−11ke≡0(modp)\sum_{k=1}^{p-1} \frac{1}{k^e} \equiv 0 \pmod{p}∑k=1p−1​ke1​≡0(modp). The trick is to look for an aaa such that multiplying every kkk by aaa shuffles the terms but rescales the whole sum.

2Hint

Use the assumption (p−1)∤e(p-1) \nmid e(p−1)∤e to find aaa with ae≢1(modp)a^e \not\equiv 1 \pmod{p}ae≡1(modp). The trick is to consider a primitive root.

Problem 11

Wolstenholme

Let p≥5p \geq 5p≥5 be a prime. Show that if

11+12+13+⋯+1p−1=mn,\frac{1}{1} + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{p-1} = \frac{m}{n},11​+21​+31​+⋯+p−11​=nm​,

then p2∣mp^2 \mid mp2∣m.

1Hint

Pair kkk with p−kp - kp−k.

2Hint

This gives ∑k=1p−11/k=p∑k=1(p−1)/21/(k(p−k))\sum_{k=1}^{p-1} 1/k = p \sum_{k=1}^{(p-1)/2} 1/(k(p-k))∑k=1p−1​1/k=p∑k=1(p−1)/2​1/(k(p−k)). We need the right-hand factor to be divisible by ppp.

3Hint

Modulo ppp, 1/(k(p−k))≡−1/k21/(k(p-k)) \equiv -1/k^21/(k(p−k))≡−1/k2. So it suffices that ∑k=1(p−1)/21/k2≡0(modp)\sum_{k=1}^{(p-1)/2} 1/k^2 \equiv 0 \pmod{p}∑k=1(p−1)/2​1/k2≡0(modp).

4Hint

Note (p−k)2≡k2(modp)(p-k)^2 \equiv k^2 \pmod{p}(p−k)2≡k2(modp), so ∑k=1p−11/k2=2∑k=1(p−1)/21/k2\sum_{k=1}^{p-1} 1/k^2 = 2 \sum_{k=1}^{(p-1)/2} 1/k^2∑k=1p−1​1/k2=2∑k=1(p−1)/2​1/k2. Can we now use a previous problem?

Comments

Obsah

  • 1Introduction
  • 2Theory
  • 2.1Recap
  • 2.2Inverses
  • 3Problems
  • Comments
MathComps LogoMathComps

The long-term vision of the project is to create a platform for beginners and advanced solvers of mathematical competitions, their tutors, and all supporters.

Navigation

  • Problems
  • Handouts
  • Guide

Project

  • About
  • Sponsors

© 2026 MathComps•Patrik Bak•Privacy and terms

MathComps LogoMathComps
ProblemsHandoutsGuideNews
Sign in