TITLE: I have a problem understanding the proof of Rencontres numbers (Derangements)
QUESTION [28 upvotes]: I understand the whole concept of Rencontres numbers but I can't understand how to prove this equation
$$D_{n,0}=\left[\frac{n!}{e}\right]$$
where $[\cdot]$ denotes the rounding function (i.e., $[x]$ is the integer nearest to $x$). This equation that I wrote comes from solving the following recursion, but I don't understand how exactly the author calculated this recursion.
$$\begin {align*} 
D_{n+2,0} & =(n+1)(D_{n+1,0}+D_{n,0}) \\
D_{0,0} & = 1 \\
D_{1,0} & = 0 
\end {align*}
$$

REPLY [58 votes]: Derangements:
A Derangement is a permutation, $P$, in which no element is mapped to itself; that is, $P(k)\ne k$, for $1\le k\le n$. Let $\mathcal{D}(n)$ be the number of derangements of $n$ items.
Here are a few methods of computing $\mathcal{D}(n)$.
Method 1 (build from smaller derangements):
Let us count the number of derangements of $n$ items so that $P(P(n))=n$.  There are $n-1$ choices for $P(n)$, and for each of those choices, $\mathcal{D}(n-2)$ ways to arrange the other $n-2$ items. Thus, there are $(n-1)\mathcal{D}(n-2)$ derangements of $n$ items so that $P(P(n))=n$.
Let us count the number of derangements of $n$ items so that $P(P(n))\not=n$. There are $n-1$ choices for $P(n)$, and for each choice, there is a derangement of $n-1$ items identical to $P$ except that they map $P^{-1}(n)\to P(n)$. Thus, there are $(n-1)\mathcal{D}(n-1)$ derangements of $n$ items so that $P(P(n))\not=n$.
Therefore,
$$
\mathcal{D}(n)=(n-1)(\mathcal{D}(n-1)+\mathcal{D}(n-2))\tag{1}
$$
Method 2 (count permutations):
Count the number of permutations of $n$ items by counting how many fix exactly $k$ items.
There are $\binom{n}{k}$ ways to choose the $k$ items to fix, then $\mathcal{D}(n-k)$ ways to arrange the $n-k$ items that are not fixed. Since there are $n!$ permutations of $n$ items, we get 
$$
n!=\sum_{k=0}^n\binom{n}{k}\mathcal{D}(n-k)\tag{2}
$$
and therefore, rearranging $(2)$ yields
$$
\mathcal{D}(n)=n!-\sum_{k=1}^n\binom{n}{k}\mathcal{D}(n-k)\tag{3}
$$
Method 3 (inclusion-exclusion):
Let $S_i$ be the set of permutations of $n$ items which fix item $i$. Then the number of permutations in $k$ of the $S_i$ would be the number of permutations that fix $k$ items.  There are $\binom{n}{k}$ ways to choose the $k$ items to fix, and $(n-k)!$ ways to arrange the other $n-k$ items. Thus, the number of permutations that fix at least $1$ item would be
$$
\sum_{k=1}^n(-1)^{k-1}\binom{n}{k}(n-k)!=\sum_{k=1}^n(-1)^{k-1}\frac{n!}{k!}\tag{4}
$$
Since there are $n!$ permutations in total, the number of permutations that don't fix any items is
$$
\begin{align}
\mathcal{D}(n)
&=n!-\sum_{k=1}^n(-1)^{k-1}\frac{n!}{k!}\\
&=\sum_{k=0}^n(-1)^k\frac{n!}{k!}\tag{5}\\
&\approx \frac{n!}{e}
\end{align}
$$
In fact, the difference
$$
\begin{align}
\left|\frac{n!}{e}-\mathcal{D}(n)\right|
&=\left|\sum_{k=n+1}^\infty(-1)^k\frac{n!}{k!}\right|\\
&=\left|\frac{1}{n+1}-\frac{1}{(n+1)(n+2)}+\frac{1}{(n+1)(n+2)(n+3)}-\dots\right|\\
&<\frac{1}{n+1}\tag{6}
\end{align}
$$
This method yields directly that $\mathcal{D}(n)$ is the closest integer to $\frac{n!}{e}$ for $n>0$.
Derivation of the Closed Form from the Recursion:
Given $\mathcal{D}(0)=1$ and $\mathcal{D}(1)=0$, and the recursion $(1)$, let's derive $(5)$. Subtracting $n\mathcal{D}(n-1)$ from both sides of $(1)$ yields
$$
\mathcal{D}(n)-n\mathcal{D}(n-1)=-(\mathcal{D}(n-1)-(n-1)\mathcal{D}(n-2))\tag{7}
$$
Using the initial conditions, $(7)$ implies
$$
\mathcal{D}(n)-n\mathcal{D}(n-1)=(-1)^n\tag{8}
$$
Dividing both sides of $(8)$ by $n!$ yields
$$
\frac{\mathcal{D}(n)}{n!}-\frac{\mathcal{D}(n-1)}{(n-1)!}=\frac{(-1)^n}{n!}\tag{9}
$$
Equation $(9)$ is very simple to solve for $\frac{\mathcal{D}(n)}{n!}$:
$$
\frac{\mathcal{D}(n)}{n!}=\sum_{k=0}^n\frac{(-1)^k}{k!}+C\tag{10}
$$
Plugging $n=0$ into equation $(10)$ yields that $C=0$. Therefore,
$$
\mathcal{D}(n)=n!\sum_{k=0}^n\frac{(-1)^k}{k!}\tag{11}
$$