TITLE: $A=\{1,2,3,4,5\}$, $B=\{0,1,2,3,4,5\}$. Find the number of one-one functions $f:A\rightarrow B$ such that $f(i)\neq i$ and $f(1)\neq0,1$
QUESTION [5 upvotes]: $A=\{1,2,3,4,5\}$, $B=\{0,1,2,3,4,5\}$. Find the number of one-one functions $f:A\rightarrow B$ such that $f(i)\neq i$ and $f(1)\neq 0\text{ or } 1$.

This is like finding the number of ways of putting $r$ letters in $r$ envelopes such that all leters are in the wrong envelops. The formula for number of such arrangements is $$r!\left(1-\frac{1}{1!}+\frac{1}{2!}-\cdots(-1)^r\frac{1}{r!}\right)$$
Here, $A$ is the envelope and $B$ is the letter. 

If $0$ is not included in the range of $f$, the number of functions is $44$ using the formula
If $1$ is not included in the range of $f$, the number of functions is also $44$ using the formula
If $0,1$ both are included in the range, I can't use the formula. How can I find the number of such possibilities?

REPLY [5 votes]: I approached it a bit differently, solving a slightly different and more general problem first and using that to get the result.
Let $A_n=\{1,\ldots,n\}$ and $B_n=\{0,1,\ldots,n\}$. Let $a_n$ be the number of one-one functions $f:A\to B$ such that $f(i)\ne i$ for each $i\in A$.

If $d_n$ is the number of derangements of $B_n$ (so that, for instance, $d_5=44$), show that $$a_n=d_n+na_{n-1}$$ for $n\ge 0$.

(We take $A_0=\varnothing$ and note that the empty function is a one-one function from $A_0$ to $B_0=\{0\}$ that has no fixed points, so $a_0=1$.) This makes it quite easy to evaluate $a_n$ recursively for small $n$.
Now your problem is a little different. You’ve already seen that there are $d_5$ one-one functions $f:A_5\to B_5$ such that $0\notin\operatorname{ran}f$. Suppose now that $f:A\to B$ is one-one, and $0$ is in the range of $f$, but $f(1)\ne 0$. There must be a $k\in\{2,3,4,5\}$ such that $f(k)=0$. The rest of $f$ must be a one-one function from $A_5\setminus\{k\}$ to $A_5$ such that $f(i)\ne i$ for $i\in A_5\setminus\{k\}$.

Explain why there are $a_4$ ways to choose a one-one function from $A_5\setminus\{k\}$ to $A_5$ such that $f(i)\ne i$ for $i\in A_5\setminus\{k\}$.
Conclude that the answer to your question is $d_5+4a_4$, and calculate it numerically.


As an aside, the numbers $a_n$ are the sequence OEIS A000255; it turns out that $a_n$ is the integer closest to
$$\frac{(n+2)n!}e\;,$$
which can be written
$$\left\lfloor\frac{(n+2)n!}e+\frac12\right\rfloor\;.$$
Added 29 January 2022: It’s not hard to generalize the argument to see that if $b_n$ is the number of one-one functions $f:A_n\to B_n$ such that $f(i)\ne i$ for $i\in A_n$ and $f(1)\ne 0$, then $b_n=d_n+(n-1)a_{n-1}$.
Since $d_n$ is the integer closest to $\frac{n!}3$, which can be written $\left\lfloor\frac{n!}e+\frac12\right\rfloor$, we have
$$b_n=\left\lfloor\frac{n!}e+\frac12\right\rfloor+(n-1)\left\lfloor\frac{(n+1)(n-1)!}e+\frac12\right\rfloor\,.$$