#title[Inclusion/Exclusion] Section 8.5+8.6 The hat problem: Suppose everyone one in a classroom has a hat, and they put their hat in a box when they enter, and then grab a random hat when they leave. What is the probability that everybody gets their own hat. Answer is $1/n!$ What about the probability that _someone_ will not get their own hat: $1-1/n!$ What about the probability that someone will get their own hat, or that no one? Need inclusion/exclusion. The probability that no one will get their own hat is $37.8%$ pretty much no matter how many people. *Remember:* $|A union B| = |A| + |B| - |A inter B|$ and $|A union B union C| = |A|+|B|+|C| - |A inter B| - |A inter C| - |B inter C| + |A inter B inter C|$ == Formula $ |A_1 union A_2 union dots union A_n| = |A_1| + |A_2| + dots + |A_n| - |A_1 inter A_2| - |A_1 inter A_3| - ... - |A_(n-1) inter A_n|\ + |A_1 inter A_2 inter A_3| + |A_1 inter A_2 inter A_4| + dots + |A_(n-2) inter A_(n-1) inter A_n)|\ - |A_1 inter A_2 inter A_3 inter A_4| - dots - |A_(n-3) inter A_(n-2) inter A_(n-1) inter A_(n)|\ + |A_1 inter A_2 inter A_3inter A_4 inter A_5| + dots + |A_(n-4) inter A_(n-3) inter A_(n-2) inter A_(n-1) inter A_(n)|\ dots dots (-1)^(n+1) |A_1 inter A_2 inter dots inter A_n| $ $ =sum_(i=1)^n |A_i|- sum_(i<=i_1=n$. $A = $ all functions of $S->B={1,2,3,dots,n}$ $A_i = $ all functions such that `i` is not in the image. $ |A\\ union_(i=1)^n A_i| = |A|-|union_(i=1)^n A_i| = n^m-mat(n;1)(n-1)^m +mat(n;2)(n-2)^m-dots (-1)^n mat(n;n)(n-n)^m $ = Permutations of an n-set {1,2,3,4} Normally a permutation of a set is the elements put on a row (like here $2,3,1,4$ and $3,1,4,2$). You can think of permutations as a bijection (different elements should be mapped to different elements, and every element needs to be mapped to an element) from ${1,2,3,4}$ to ${1,2,3,4}$: $ mat(1,2,3,4;2,3,1,4) $ Notice the 4, mapped unto itself, it's called a fixed point. The 4's would also be a fixed point here: $ mat(1,2,4,3;2,3,4,1) $ But then its a different set at the top. == Derangement A bijection ($pi$) of a set `S` unto itself with no fixed points. That is $pi(i) eq.not i$ The hat problem: to find the chances of noone getting their own hat, just use derangements. == Counting permutations $A$ is the set of all permutations of ${1,2,dots,n}$ $A_i =$ the set of permutations $pi$ such that $pi(i)=i$ $ D_n &=|A\\(union_(i=1)^n) = |A| - sum_(i=1)^n + sum_(i_1