Overview
Combinatorics deals with counting arrangements and selections. Understanding the difference between permutations (order matters) and combinations (order doesn’t matter) is crucial.Key Concepts
- Permutations: Arrangements where order matters
- Combinations: Selections where order doesn’t matter
- With/Without Repetition: Whether elements can be reused
Combinations - C(n,k)
Number of ways to choose k items from n items (order doesn’t matter).Formula
O(k) Direct Computation
O(1) with Preprocessing (Using Factorials)
Modular nCr (Compressed Version)
Permutations - P(n,k)
Number of ways to arrange k items from n items (order matters).Formula
- P(5,2) = 5×4 = 20 (first position: 5 choices, second: 4 choices)
- P(5,3) = 5×4×3 = 60
Formulas Summary
| Type | Formula | Meaning |
|---|---|---|
| C(n,k) | n!/(k!(n-k)!) | Combinations without repetitions |
| H(n,k) | C(n+k-1, k) | Combinations with repetitions |
| P(n,k) | n!/(n-k)! | Permutations without repetitions |
| P(n,{m1,m2,…}) | n!/(m1!m2!…) | Permutations with repetitions |
Properties
Combination Properties
Catalan Numbers
- Valid parentheses sequences
- Binary search trees with n nodes
- Paths in grid that don’t cross diagonal
- Ways to triangulate a polygon
Catalan Number Applications
Catalan Number Applications
Example 1: Valid ParenthesesC(3) = 5 ways to arrange 3 pairs of parentheses:
((()))(()())(())()()(())()()()
k-th Permutation
Finds the k-th permutation in lexicographic order.Next Combination
Generates all combinations iteratively.STL Permutations
Contest Examples
Example 1: Count Ways to Select Team
Example 2: Paths in Grid
Example 3: Distributing Identical Items
Example 4: Derangements
Common Patterns
Stars and Bars
Distribute k identical items into n distinct bins:- At least 0 in each: H(n,k) = C(n+k-1, k)
- At least 1 in each: C(k-1, n-1)
Inclusion-Exclusion
Count arrangements avoiding certain conditions:Pigeonhole Principle
If n items are placed into m containers with n > m, at least one container contains more than one item.Complexity Summary
| Operation | Time | Space |
|---|---|---|
| C(n,k) direct | O(k) | O(1) |
| C(n,k) with factorial | O(1)* | O(n) |
| All combinations C(n,k) | O(C(n,k)) | O(k) |
| All permutations | O(n! × n) | O(n) |
| k-th permutation | O(n²) | O(n) |