Maths WorkshopMaths problems with full solutions
Exercise 2226Combinatorics › Counting principlesOlympiadAdded 4 Nov 2026

Subsets whose sum is divisible by p

#roots of unity filter#generating functions#counting#modular arithmetic

Statement

Let pp be an odd prime. Prove that the number of subsets of {1,2,…,p}\{1,2,\dots,p\} whose sum of elements is divisible by pp is
2p+2(p−1)p.\frac{2^p+2(p-1)}p.

Hint

Let ω=e2πi/p\omega=e^{2\pi i/p}. Then 1p∑j=0p−1ωjt\frac1p\sum_{j=0}^{p-1}\omega^{jt} equals 11 if p∣tp\mid t and 00 otherwise. Apply this to the sum of each subset and exchange the order of summation.

Show solutionHide solution

Solution

Roots of unity filter. Let NN be the number we want. For a subset AA with sum Σ(A)\Sigma(A),
1p∑j=0p−1ωjΣ(A)={1p∣Σ(A)0otherwise,\frac1p\sum_{j=0}^{p-1}\omega^{j\Sigma(A)}=\begin{cases}1&p\mid\Sigma(A)\\0&\text{otherwise,}\end{cases}
so
N=1p∑j=0p−1∑AωjΣ(A)=1p∑j=0p−1∏k=1p(1+ωjk).N=\frac1p\sum_{j=0}^{p-1}\sum_{A}\omega^{j\Sigma(A)}=\frac1p\sum_{j=0}^{p-1}\prod_{k=1}^p\bigl(1+\omega^{jk}\bigr).

The term j=0j=0 gives 2p2^p.

The terms j=1,…,p−1j=1,\dots,p-1. The residues jkjk modulo pp run over all residues exactly once, so
∏k=1p(1+ωjk)=∏r=0p−1(1+ωr).\prod_{k=1}^p(1+\omega^{jk})=\prod_{r=0}^{p-1}(1+\omega^r).
From ∏r=0p−1(x−ωr)=xp−1\prod_{r=0}^{p-1}(x-\omega^r)=x^p-1 at x=−1x=-1:
∏r=0p−1(1+ωr)=2.\prod_{r=0}^{p-1}(1+\omega^r)=2.

Conclusion.
N=1p(2p+(p−1)⋅2)=2p+2(p−1)p.N=\frac1p\bigl(2^p+(p-1)\cdot2\bigr)=\frac{2^p+2(p-1)}p.

Check. p=3p=3: 8+43=4\frac{8+4}3=4. p=5p=5: 88. p=7p=7: 2020.

Remark. As a corollary, 2p≡2(modp)2^p\equiv2\pmod p, a proof of Fermat's little theorem for base 22.

Lesson. To count objects with a divisibility condition, average a root of unity over the residues.