Problem. How many permutations of elements have no fixed point ( for all )?
The formulas
By inclusion-exclusion, over which elements are fixed:
Two recurrences:
Asymptotically:
So a random permutation has no fixed point about 37% of the time, essentially independent of β the surprising part.
long long derangement(int n, long long MOD) {
if (n == 0) return 1;
if (n == 1) return 0;
long long a = 1, b = 0; // D(0), D(1)
for (int i = 2; i <= n; i++) {
long long c = (long long)(i - 1) % MOD * ((a + b) % MOD) % MOD;
a = b; b = c;
}
return b;
}The first recurrence, explained
Element 1 goes to some position ( choices). Then either:
- goes to position 1 β the remaining elements form a derangement: ;
- does not go to position 1 β treat βposition 1β as βs forbidden spot, giving a derangement of elements: .
Hence .
Permutations with exactly fixed points
Choose which are fixed, derange the rest. Summing over recovers β a binomial-transform identity.
Expected number of fixed points is exactly 1, for every , by linearity of expectation: each element is fixed with probability .
The generalisation: forbidden positions
Derangements forbid one position per element. The general problem β a permutation avoiding an arbitrary set of forbidden pairs β is counted by the permanent of the 0/1 allowed-matrix, which is p-hard. But:
| Structure of the forbidden set | Method |
|---|---|
| One forbidden position each (derangement) | closed form |
| Forbidden positions form a board with rook-polynomial structure | rook polynomials + inclusion-exclusion |
| Small | bitmask DP (Ryserβs formula) |
| Bipartite structure | it is counting perfect matchings β p-hard |
| Forbidden positions form a staircase | MΓ©nage-like formulas |
Rook polynomials
If the forbidden cells form a board , and counts the ways to place non-attacking rooks on , then
For derangements the board is the diagonal, , and the formula collapses to the inclusion-exclusion above.
Related counts
| Problem | Answer |
|---|---|
| Derangements | |
| Exactly fixed points | |
| MΓ©nage problem (couples, alternating, no adjacent partners) | a separate formula |
| Permutations with no mapping to or | |
| Involutions () | |
| Permutations with all cycles | derangements |
| Permutations with exactly cycles | Stirling numbers of the first kind |
| Random permutation has a cycle of length | probability |
The last row is a nice companion fact, and it is exactly the analysis behind the 100 prisoners problem.
The hat-check problem
people check hats; the hats are returned at random. The probability nobody gets their own is .
It is the standard illustration that inclusion-exclusion over βat least one fixed pointβ gives an alternating series converging to β and that the answer barely depends on (already accurate to three decimals at ).
Why it is worth knowing
Derangements are the smallest non-trivial inclusion-exclusion computation, and the limit is a memorable example of a combinatorial probability converging to a constant. The βexactly fixed pointsβ formula is also a frequently needed building block in counting problems.
See also: Combinatorics Β· Special Numbers Β· Expected Value