[Tutorial] Inclusion-Exclusion Principle, Part 1.

Revision en21, by Roundgod, 2019-03-22 10:27:48

Hello, Codeforces! The reason why I am writing this blog is that my ACM/ICPC teammate calabash_boy is learning this technique recently(he is a master in string algorithms,btw), and he wanted me to provide some useful resources on this topic. I found that although many claim that they do know this topic well, problems concerning inclusion-exclusion principle are sometimes quite tricky and not that easy to deal with. Also, after some few investigations, the so-called "Inclusion-Exclusion principle" some people claim that they know wasn't the generalized one, and has little use when solving problems. So, what I am going to pose here, is somewhat the "Generalized Inclusion-Exclusion Principle". Most of the describing text are from the graduate text book Graduate Text in Mathematics 238, A Course in Enumeration, and the problems are those that I encountered in real problem set, so if possible, I'll add a link to the real problem so that you can solve it by yourself. I'll start with the basic formula, one can choose to skip some of the text depending on your grasp with the topic.

Consider a finite set and three subsets , To obtain , we take the sum + + . Unless are pairwise distinct, we have an overcount, since the elements of has been counted twice. So we subtract . Now the count is correct except for the elements in which have been added three times, but also subtracted three times. The answer is therefore

, or equivalently,

The following formula addresses the case applied to more sets.

The Restricted Inclusion-Exclusion Principle. Let be subsets of . Then

This is a formula which looks familiar to many people, I'll call it The Restricted Inclusion-Exclusion Principle, it can convert the problem of calculating the size of the union of some sets into calculating the size of the intersection of some sets. It's not hard to prove the correctness of this formula, we can just check how often an element is counted in both sides. If , then it's counted once on either side. Suppose , and more precisely, that is in exactly of the sets . The count on the left-hand side is , and on the right hand side, we have

for , thus the equality holds.

Example 1. Let's see an example problem Co-prime where this principle could be applied: Given , you need to compute the number of integers in the interval such that is coprime with , that is, . There are testcases. Constraints: , .

Solution

The standard interpretation leads to the principle of inclusion-exclusion. Suppose we are given a set , called the universe, and a set of properties that the elements of may or may not process. Here we can define the properties as we like, such as , , or even . Let be the subset of elements that enjoy property (and possibly others). Then is the number of elements that process none of the properties. Clearly, is the set of elements that possess the properties (and maybe others). Using the notation

we arrive at the inclusion-exclusion principle.

Inclusion-Exclusion Principle. Let be a set, and a set of properties. Then

The formula becomes even simpler when depends only on the size . We can then write for , and call a homogeneous set of properties, and in this case also depends only on the cardinality of . Hence for homogeneous properties, we have

This is the very essence of Inclusion-Exclusion Principle . Please make sure you understand every notation before you proceed. One can figure out, by letting , we arrive at the restricted inclusion-exclusion principle.

Example 2. This problem Character Encoding requires you to compute the number of solutions to the equation , satisfying that , modulo . Constraints: . Hint: the number of non-negative integer solutions to is given by .

Solution

Example 3. Well, this one is a well-known problem. K-Inversion Permutations. The statement is neat and simple. Given N, K, you need to output the number of permutations of length N with K inversions, taken modulo . Constraint: .

Solution

Example 4. This problem comes from XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Gomel.Problem K,(Yes, created by tourist:) ) which gives a integer , and requires one to find out the number of non-empty sets of positive integers, such that their greatest common divisor is , and their least common multiple is , taken modulo .Constraint: .

Solution

I guess that's the end of this tutorial. IMO, understanding all the solutions to the example problems above is fairly enough to solve most of the problems that require the inclusion-exclusion principle(but only for the IEP part XD). This is my first time of writing an tutorial. Please feel free to ask any questions in the comments below or point out any mistakes in the blog.

#### History

Revisions

Rev. Lang. By When Δ Comment
en27 Roundgod 2022-01-07 09:58:51 9
en26 Roundgod 2020-11-01 10:27:44 2
en25 Roundgod 2020-10-13 21:06:15 4 Tiny change: 'irwise distinct, we have' -> 'irwise disjoint, we have'
en24 Roundgod 2019-03-23 21:22:54 8 Tiny change: '-j,j)+g(i-1,j-1)-g(i-j-N,j-1)$. An' -> '-j,j)+g(i-j,j-1)-g(i-(N+1),j-1)$. An'
en23 Roundgod 2019-03-22 11:04:49 13 Tiny change: 'g(i-1,j-1)$. An' -> 'g(i-1,j-1)-g(i-j-N,j-1)$. An'
en22 Roundgod 2019-03-22 10:30:16 2 Tiny change: 'ts_{i\geq 1}(-1)^{i}g' -> 'ts_{i\geq 0}(-1)^{i}g'
en21 Roundgod 2019-03-22 10:27:48 2 Tiny change: '-j,j)+g(i-j,j-1)$. An' -> '-j,j)+g(i-1,j-1)$. An'
en20 Roundgod 2019-01-19 16:24:31 2 Tiny change: '\n$$N_{\subseteq T}:=' -> '\n$$N_{\supseteq T}:='
en19 Roundgod 2019-01-19 10:10:02 2 Tiny change: '1}\cdot f(i)$$\n\nwhe' -> '1}\cdot f(s)$$\n\nwhe'
en18 Roundgod 2019-01-18 23:20:27 5
en17 Roundgod 2019-01-18 22:39:48 1 Tiny change: ' for the $\relax i$th eleme' -> ' for the $i$th eleme'
en16 Roundgod 2019-01-18 21:44:53 7 Tiny change: '}"$. Let$A_i$be th' -> '}"$. Let $\relax A_i$ be th'
en15 Roundgod 2019-01-18 21:34:57 697 (published)
en14 Roundgod 2019-01-18 21:27:56 1521
en13 Roundgod 2019-01-18 21:06:41 613
en12 Roundgod 2019-01-18 20:59:52 2119 Tiny change: 'ution is $O(n)$, due' -> 'ution is $\reO(n)$, due'
en11 Roundgod 2019-01-18 20:23:49 1028 Tiny change: 'hen write $N_{\sups' -> 'hen write \n$N_{\sups'
en10 Roundgod 2019-01-18 20:01:13 2 Tiny change: '\leq x_{i}\< m$? Cons' -> '\leq x_{i} < m$? Cons'
en9 Roundgod 2019-01-18 20:00:40 1594 Tiny change: 'clusion.\n**Princi' -> 'clusion.\n\n**Princi'
en8 Roundgod 2019-01-18 19:36:24 1162
en7 Roundgod 2019-01-18 19:16:44 1245 Tiny change: 'm}=0$$for m\geq 1,' -> 'm}=0$$ for$m\geq 1$,'
en6 Roundgod 2019-01-18 18:49:31 699
en5 Roundgod 2019-01-18 18:41:06 583
en4 Roundgod 2019-01-18 18:21:38 58
en3 Roundgod 2019-01-18 18:17:55 472
en2 Roundgod 2019-01-18 18:14:24 423
en1 Roundgod 2019-01-18 18:09:36 831 Initial revision (saved to drafts)