PrepStack · Markdown HTML

Combinatorial counting / inclusion-exclusion

Math, number theory, and combinatorics · HTML

Formal shape

Multiply independent stages; unordered choice gives C(n,k); overlapping properties use |A∪B|=sum singles-sum intersections+... .

Recognition signals

False friends

SignalWhy it is different
Selections have strong state dependenceIf one choice changes future options, DP is usually safer than a closed combination formula.
Applying permutations to indistinguishable objectsClarify labels, repetition, and ordering before choosing a formula.

Core invariant

Every valid outcome maps to one counting representation, or to a known fixed multiplicity that is explicitly corrected.

State, transition, and processing order

ItemDefinition
StateCategory sizes, remaining choices, binomial or factorial tables, and inclusion-exclusion subsets.
TransitionMultiply independent stages, add disjoint cases, and alternate signs over intersections when cases overlap.
Frontier / orderProcessed categories are compressed to a count; unprocessed categories affect only remaining parameters.

Python skeleton

def binomial_table(n):
    c = [[0]*(n+1) for _ in range(n+1)]
    for i in range(n+1):
        c[i][0] = c[i][i] = 1
        for k in range(1, i):
            c[i][k] = c[i-1][k-1] + c[i-1][k]
    return c

def inclusion_exclusion(universe_size, bad_intersections):
    # map nonempty subset mask -> size of intersection of those bad sets
    good = universe_size
    for mask, size in bad_intersections.items():
        bits = mask.bit_count()
        good += (-1 if bits % 2 else 1) * size
    return good

Correctness sketch

  1. Pascal recurrence partitions choices by whether one designated object is selected.
  2. The product rule bijects each outcome with one tuple of stage choices.
  3. In inclusion-exclusion, an object in k bad sets has total bad-union contribution one after alternating binomial terms.
  4. An object in no bad set retains only its initial good contribution.

Complexity

TimeSpaceParameters
O(n²) for a binomial table; O(2^k) for k-property inclusion-exclusionO(n²), reducible to O(n) for one rown object scale and k inclusion-exclusion properties

Edge cases

Error patterns

PatternWhy it fails / fix
Confusing permutations and combinationsAsk whether swapping selected objects creates a new outcome.
Adding overlapping cases directlyShared outcomes are counted repeatedly without inclusion-exclusion.
Ordinary division under a modulusModular division requires an existing inverse.

Selection and exclusion rules

RelationTemplateBoundary
alternativeDigit dynamic programmingThere is no tight bound or direct combinatorics captures the property.
enumerative alternativeSubset / combination backtrackingConcrete outcomes must be emitted

Original micro-example

PartDetail
ResultC(5,2)×3=30 outcomes.
ScenarioChoose two unordered teas from five, then one cup from three.
WalkthroughTea order adds no outcome; cup choice is independent, so stage counts multiply.

Recall prompts

My notes