from the wiki, a way to count violations,
Suppose there are n people, numbered 1, 2, ..., n. Let there be n hats also numbered 1, 2, ..., n. We need to find the number of ways in which no one gets a hat with the same number as his number. Let us assume that the first person takes hat i. There is an n - 1 way for the first person to make that choice. Now there are two possibilities, depending on whether the person I accept hat 1 in return:
Person I do not accept hat 1. This case is equivalent to solving the problem with n - 1 people and n - 1 heads: each of the remaining n - 1 people has exactly 1 forbidden choice from the remaining n - 1 hat (prohibited choice is hat 1). Person I take hat 1. Now the problem boils down to n - 2 faces and n - 2 hats.
This implies the following relationship:
!n = (n - 1) (!(n-1) + !(n-2))
Here I do not understand the second part. I'm trying to think about such a problem,
NO.1: I am a man, so I can not take this hat. Therefore, I have option n-1. This reduces the problem with an n-1 character with an n-1 hat, which will be multiplied (n-1) times.
But I can not understand the second part of the recursive call. from the aisle, "man, I take hat 1", how ...? Is it not true that "I have banned hat 1"? Then, how will a person take hat 1. Otherwise, if "i forbidden hat is not 1", then this does not reduce to NO.1?
So more or less I find it difficult to understand this part of the recursive call,
!n = (n - 1) (!(n-1) + !(n-2))