Probability on balls and cells and their expected values n balls that are numbered 1, 2, ..., n are randomly distributed into n cells that are also numbered 1, 2, ..., n. Every cell can place exactly 1 ball. Let X be the number of balls that were put into cells with identical numbers. Compute E(X).
So from what I understand so far, I would have to find the general formula of P(X = k) for k = 1,2,...n
For P(X = n) it is quite simple which is 1/n!
For P(X = k), 1 <= k < n, I am stuck as I have only figured out a way of choosing the cells which are identical with the numbers on the ball which is just n choose k. What about the rest of the cells? For example, we have 5 cells. 2 of the cells are going to be identical with the number on the balls. That is just 5 choose 2. What about the other 3 cells? If using brute force, out of the 6 ways of putting the balls into the cells, only 2 would suit our condition. How do we find the general formula for it?
Sorry if my notation is not correct but I hope you understand what I am asking.
 A: The Simple Solution Using Linearity of Expectation
In each cell, the expected number of balls with the correct number is $\frac1n$. There are $n$ cells, so by Linearity of Expectation, the expected number is $n\cdot\frac1n=1$.

A More Deranged Answer
The number of Derangements of $n$ items is
$$
\mathcal{D}(n)=\sum_{j=0}^n(-1)^j\frac{n!}{j!}\tag{1}
$$
The number of ways to have $k$ items in the right place is
$$
\binom{n}{k}\mathcal{D}(n-k)\tag{2}
$$
that is, the number of ways to choose the $k$ items times the number of derangements of the rest.
Because the following sum counts the total number of permutations of $n$ items, we have
$$
\sum_{k=0}^n\binom{n}{k}\mathcal{D}(n-k)=n!\tag{3}
$$
Thus, the expected number of balls in the correct cells would be
$$
\begin{align}
\frac1{n!}\sum_{k=0}^nk\binom{n}{k}\mathcal{D}(n-k)
&=\frac1{n!}\sum_{k=1}^nn\binom{n-1}{k-1}\mathcal{D}((n-1)-(k-1))\tag{4}\\
&=\frac1{(n-1)!}(n-1)!\tag{5}\\[9pt]
&=1\tag{6}
\end{align}
$$
Explanation:
$(4)$: $k\binom{n}{k}=n\binom{n-1}{k-1}$
$(5)$: apply $(3)$ to $n-1$
$(6)$: divide

The Variance
Using the same, deranged method as above, we get
$$
\begin{align}
\frac1{n!}\sum_{k=0}^nk^2\binom{n}{k}\mathcal{D}(n-k)
&=\frac1{n!}\sum_{k=0}^n(k(k-1)+k)\binom{n}{k}\mathcal{D}(n-k)\\
&=\frac1{n!}\sum_{k=2}^nn(n-1)\binom{n-2}{k-2}\mathcal{D}((n-2)-(k-2))\\
&+\frac1{n!}\sum_{k=1}^nn\binom{n-1}{k-1}\mathcal{D}((n-1)-(k-1))\\
&=\frac1{(n-2)!}(n-2)!+\frac1{(n-1)!}(n-1)!\\[9pt]
&=2\tag{7}
\end{align}
$$
The Variance is the mean of the square minus the square of the mean. Thus, the variance is $2-1=1$.
