<s>[INST] Combinatorics, surjective functions with conditions Question: $A=\left\{ 1,2,3,4,5\right\} $
  , $B=\left\{ 1,2,3\right\} $
 . How many surjective functions are there such that $ f(1)\neq1$
 ,$f(2)\neq2$
 ,$ f(3)\neq2$
  .
Solution: Overall we have $3^{5}-{3 \choose 1}2^{5}+{3 \choose 2}1^{5}=150$
  functions that is surjective. We will denote $A_{i,j}=\left\{ f:A\rightarrow B\,|\, f\,\text{is surjective and }f(i)=j\right\} $
 We will calculate $\left|A_{1,1}\cup A_{2,2}\cup A_{3,2}\right|$
  then subtract it from 150.
So $\left|A_{1,1}\right|=\left|A_{2,2}\right|=\left|A_{3,2}\right|=3^{4}-{3 \choose 1}2^{4}+{3 \choose 2}1^{4}=36\Rightarrow\left|A_{1,1}\right|+\left|A_{2,2}\right|+\left|A_{3,2}\right|=3.36=108$
and $\left|A_{1,1}\cap A_{2,2}\right|=\left|A_{1,1}\cap A_{3,2}\right|=\left|A_{2,2}\cap A_{3,2}\right|=3^{3}-{3 \choose 1}2^{3}+{3 \choose 2}1^{3}=6\Rightarrow\left|A_{1,1}\cap A_{2,2}\right|+\left|A_{1,1}\cap A_{3,2}\right|+\left|A_{2,2}\cap A_{3,2}\right|=3.6=18$
lastly $\left|A_{1,1}\cap A_{2,2}\cap A_{3,2}\right|=3.2=6$
  because we have 4 or 5 has to go to 3, otherwise we wouldn't have a surjective functions. 
So my answer was $150-108+18-6=54$
 . However I was told that the solution was 45. I would appreciate if someone can tell me where my mistake is. Thank you!
 [/INST] As an alternate method, let $A_i$ be the preimage of $i$ for $1\le i\le3$.
$\textbf{1)}$ If $1\in A_2$, 
then there are $3\cdot3=9$ possible distributions of 4 and 5 if $2\in A_i$ and $3\in A_j$ with $i\ne j$
and there are $3^2-2^2=5$ possible distributions of 4 and 5 if $2, 3 \in A_i$ for $i=1$ or $i=3$.
$\textbf{2)}$ If $1\in A_3$,
then there are $3^2-2^2=5$ possible distributions of 4 and 5 if $2\in A_1$ or $3\in A_1$
and there are $2\cdot1=2$ possible distributions of 4 and 5 if $2,3\in A_3$.
Thus there are $2\cdot9+2\cdot5+3\cdot5+1\cdot2=45$ such functions.
</s>