An alternate solution to the hat matching problem, probability of **exactly** k matches

inclusion-exclusionprobability

I have a question on the famous hat matching problem. Here's the setup: $N$ people arrive at a party all of whom are wearing hats. We collect all the hats and then redistribute them. What is the probability that exactly $k$ of the party members receive their own hats back?

Best Answer

The "hat problem" is really getting the number $D_n$ of derangements of $\{1, 2, \dotsc, n\}$. What you are asking is the number of permutations with exactly $k$ fixed points. Now a permutation with exactly $k$ fixed is constructed by:

  • Pick the $k$ fixed points, there are $\binom{n}{k}$ ways to do so
  • The other $n - k$ elements are not on their places, i.e., they form a derangement of $n - k$ elements, i.e., $D_{n - k}$.

Total is just $\binom{n}{k} D_{n - k}$