Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Math & Number Theory/Combinatorics mod p (nCr, Catalan, stars and bars)

Technique

Inclusion-Exclusion Principle

Count a union by alternately adding and subtracting intersections, often iterating over subsets of a small set of conditions.

Learn firstCombinatorics mod p (nCr, Catalan, stars and bars)Bit Tricks & Bitmasking
0%
mastery
0/9 solved
Practise nextCounting Coprime Pairs
IdeaPracticePitfalls

Recognise it

The signal

count numbers divisible by at least one of k primes

  1. 1Brute force

    Enumerate every object and check.

  2. 2The observation

    Enumerating objects: intersections of conditions are easy to count.

  3. 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

Next

Counting Coprime Pairs

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

Variants and techniques

  • Mobius Inversion & Multiplicative FunctionsCP 2000Count pairs or subsets by gcd in two steps. First, for every d, count the items divisible by d (a harmonic sum, O(V log V)). Then invert: the exactly-gcd-1 count is the sum of mu(d)*f(d). A linear sieve gives mu, phi and divisor functions.

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

    Counting Coprime PairsGCD SubsetsCoprime Subsequences

Resources

  • Binary exponentiationcp-algorithms · article
  • Sieve of Eratosthenescp-algorithms · article
  • Modular inversecp-algorithms · article
  • DivisibilityUSACO Guide · course
  • CombinatoricsUSACO Guide · course
  • Math cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems