Technique
Inclusion-Exclusion Principle
Count a union by alternately adding and subtracting intersections, often iterating over subsets of a small set of conditions.
0%
mastery
0/9 solved
0/9 solved
Recognise it
The signal
count numbers divisible by at least one of k primes
1Brute force
Enumerate every object and check.
2The observation
Enumerating objects: intersections of conditions are easy to count.
3Why it works
Each object is counted net exactly once across the signed terms.
Reach for it when
- +count objects avoiding all bad properties
- +coprime pairs via Mobius-like sums
Not when
- ×intersections are hard to count
- ×too many conditions without structure (Mobius / sieve)
Practice
0/9 solved
- Counting Coprime PairsCSESMedium
- Counting PermutationsCSESMedium
- Prime MultiplesCSESMedium
- Christmas PartyCSESMedium
- Coprime SubsequencesCodeforces2000
- And Subset CountCSESHard
- Counting SequencesCSESHard
- Grid CompletionCSESHard
- Counting ReordersCSESHard
Pitfalls
Misconceptions
- !wrong sign for even/odd subset size
Go deepervariants and techniques · 1 combinations · resources
Combinations
Mobius Inversion & Multiplicative Functions+Sieve of Eratosthenes / Smallest Prime Factor+Combinatorics mod p (nCr, Catalan, stars and bars)
count pairs/subsequences with gcd 1 (or gcd exactly g), values <= 1e6
- Mobius Inversion & Multiplicative Functions:
- invert 'divisible by d' counts into 'gcd exactly 1'
- Sieve of Eratosthenes / Smallest Prime Factor:
- a linear sieve for mu (and the multiples counts)
- Combinatorics mod p (nCr, Catalan, stars and bars):
- count pairs C(c_d, 2) or subsets 2^c_d - 1 from c_d multiples