A deep dive into the Inclusion-Exclusion Principle (IEP) in competitive programming with examples of 2-way, 3-way, and 4-way applications.
The Inclusion-Exclusion Principle (IEP) is a fundamental combinatorial technique frequently used in competitive programming to efficiently count elements satisfying at least one condition. This principle is particularly useful when direct iteration is inefficient due to a large input size.
Core Idea: Imagine counting people at a party. If some individuals belong to multiple friend circles, they might be counted more than once. The Inclusion-Exclusion Principle helps correct this by subtracting overlaps, ensuring each person is counted only once.
Given two sets:
We need to determine how many numbers in the range [1, N]
are divisible by either 2 or 3.
N = 20
S2 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} <-- Multiples of 2
S3 = {3, 6, 9, 12, 15, 18} <-- Multiples of 3
S2 β© S3 = {6, 12, 18} <-- Common multiples (LCM(2,3) = 6)
Formula:
|Sβ βͺ Sβ| = |Sβ| + |Sβ| - |Sβ β© Sβ|
Calculations:
|S2| = floor(20 / 2) = 10
|S3| = floor(20 / 3) = 6
|S2 β© S3| = floor(20 / 6) = 3 <-- Multiples of LCM(2,3)
Result:
|Sβ βͺ Sβ| = 10 + 6 - 3 = 13
The union of Set A and Set B will have 5 elements in total. This is because the intersection of Set A and Set B has 2 elements, which are counted twice when we add the sizes of Set A and Set B. So, we subtract the size of the intersection to avoid double counting.
We can expand this principle for three sets:
3-way IEP |A βͺ B βͺ C| =
+ (|A| + |B| + |C|)
- (|A β© B| - |A β© C| - |B β© C|)
+ (|A β© B β© C|)
If we apply the same logic, the pattern continues as we introduce more sets.
Applying four-way Inclusion-Exclusion:
4-way IEP |A βͺ B βͺ C βͺ D| =
+ (|A| + |B| + |C| + |D|)
- (|A β© B| + |A β© C| + |A β© D| + |B β© C| + |B β© D| + |C β© D|)
+ (|A β© B β© C| + |A β© B β© D| + |A β© C β© D| + |B β© C β© D|)
- |A β© B β© C β© D|
The pattern of + and - signs continues similarly for five-way, six-way, and beyond.
For a five-way set union, the formula expands further:
5-way IEP |A βͺ B βͺ C βͺ D βͺ E| =
+ (
|A| + |B| + |C| + |D| + |E|
)
- (
|A β© B| + |A β© C| + |A β© D| + |A β© E| +
|B β© C| + |B β© D| + |B β© E| + |C β© D| +
|C β© E| + |D β© E|
)
+ (
|A β© B β© C| + |A β© B β© D| + |A β© B β© E| + |A β© C β© D| +
|A β© C β© E| + |A β© D β© E| + |B β© C β© D| + |B β© C β© E| +
|B β© D β© E| + |C β© D β© E|
)
- (
|A β© B β© C β© D| + |A β© B β© C β© E| + |A β© B β© D β© E| +
|A β© C β© D β© E| + |B β© C β© D β© E|
)
+ (
|A β© B β© C β© D β© E|
)
πππβ¦ β the princess πΈ was counting. She had trouble falling asleep π΄, and she got bored of counting lambs π when she was nine. However, just counting dragons was boring as well, so she entertained herself as best she could. Tonight she imagined that all dragons were here to steal her π°, and she was fighting them off. Every k-th dragon got punched in the face with a frying pan π³. Every l-th dragon got his tail shut into the balcony door πͺ. Every m-th dragon got his paws trampled with sharp heels π . Finally, she threatened every n-th dragon to call her mom π, and he withdrew in panic π¨. How many imaginary dragons suffered moral or physical damage tonight, if the princess counted a total of d dragons? ππ₯
k1 = 2 (punched every 2 days)
k2 = 3 (punched every 3 days)
N = 12
{2, 3, 4, 6, 8, 9, 10, 12}
8
Affected Days: {2, 3, 4, 6, 8, 9, 10, 12}
Affected by k: {2, 4, 6, 8, 10, 12}
Affected by l: {3, 6, 9, 12}
Affected by k and l: {6, 12}
Affected by k or l: {2, 3, 4, 6, 8, 9, 10, 12}
Affected by k or l: 8
// Brute Force Approach
class SolutionBruteForce {
public:
int solve(int k, int l, int m, int n, int d) {
if (k == 1 || l == 1 || m == 1 || n == 1) {
return d;
}
if (min({k, l, m, n}) > d) {
return 0;
}
int count = 0;
for (int i = 1; i <= d; i++) {
if (i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0) {
count++;
}
}
return count;
}
};