dr_nullptr's blog

By dr_nullptr, 82 minutes ago, In English

A lot of permutation counting problems contain a restriction that looks like

$$$ p_i\neq i. $$$

Sometimes this restriction is required for every position, sometimes only for a few positions, and sometimes the statement hides it behind something like "exactly $$$k$$$ elements move".

The completely symmetric case is called a derangement.

A permutation $$$p$$$ of $$${1,2,\ldots,n}$$$ is a derangement if

$$$ p_i\neq i $$$

for every $$$i$$$.

I will denote the number of derangements of size $$$n$$$ by $$$D_n$$$.

For example, for $$$n=3$$$ the only derangements are

$$$ [2,3,1],\qquad [3,1,2], $$$

so $$$D_3=2$$$.

The first few values are

$$$ D_0=1,\quad D_1=0,\quad D_2=1,\quad D_3=2,\quad D_4=9,\quad D_5=44,\ldots $$$

The formula for $$$D_n$$$ is classical, but IMO the more interesting part is how we get it, because the same reasoning survives in much less symmetric permutation problems.

Starting with a small case

Before writing a general formula, consider $$$n=4$$$.

There are $$$4!=24$$$ permutations in total, and we want to remove every permutation having at least one fixed point.

Suppose position $$$1$$$ is fixed. The remaining three elements can be permuted arbitrarily, so there are $$$3!$$$ such permutations. The same is true for every position, which makes

$$$ 4!-4\cdot3! $$$

a tempting first guess.

It is not correct, because a permutation having two fixed points was subtracted twice. We have to add these permutations back.

There are $$$\binom42$$$ ways to choose two fixed positions, and after fixing them the remaining two elements can be permuted in $$$2!$$$ ways.

Now permutations having three fixed points are counted incorrectly again, so we subtract them. Continuing the same correction gives

$$$ \begin{aligned} D_4 &=4!-\binom41 3!+\binom42 2!-\binom43 1!+\binom44 0!\\ &=24-24+12-4+1\\ &=9. \end{aligned} $$$

This is exactly the kind of repeated overcounting that the Inclusion-Exclusion Principle is designed for.

Generalizing the previous argument

For every $$$i$$$, define

$$$ A_i=\{p:p_i=i\}. $$$

So $$$A_i$$$ is the set of permutations in which position $$$i$$$ is fixed.

A derangement belongs to none of

$$$ A_1,A_2,\ldots,A_n. $$$
Derivation

Hence the usual closed form is

$$$ D_n = n! \left( 1-\frac1{1!}+\frac1{2!}-\frac1{3!}+\cdots+\frac{(-1)^n}{n!} \right). $$$

The formula is useful, but there is another way to count the same permutations which gives a very convenient recurrence.

Looking at the cycle containing $$$1$$$

Take a derangement of size $$$n$$$ and look at the value in position $$$1$$$.

Since $$$1$$$ cannot remain fixed, suppose

$$$ p_1=j,\qquad j\neq1. $$$

There are $$$n-1$$$ possible choices for $$$j$$$.

After choosing $$$j$$$, exactly one of the following conditions holds:

$$$ \begin{cases} p_j=1,\\ p_j\neq1. \end{cases} $$$
Proof

The recurrence is therefore

$$$ D_n=(n-1)(D_{n-1}+D_{n-2}), $$$

with initial values $$$D_0=1$$$ and $$$D_1=0$$$. The first few values follow immediately:

$$$ \begin{aligned} D_2&=1,\\ D_3&=2(D_2+D_1)=2,\\ D_4&=3(D_3+D_2)=9,\\ D_5&=4(D_4+D_3)=44. \end{aligned} $$$

Why is $$$D_0=1$$$?

Defining $$$D_0=1$$$ may look artificial at first, but there is exactly one permutation of the empty set: the empty permutation.

This convention also makes the combinatorial formulas behave naturally. For example, the number of permutations with all $$$n$$$ positions fixed is

$$$ \binom nnD_0=1, $$$

which is exactly the identity permutation.

Another recurrence falls out of Inclusion-Exclusion

The Inclusion-Exclusion formula also gives a surprisingly short recurrence:

$$$ D_n=nD_{n-1}+(-1)^n. $$$
Derivation

Equivalently,

$$$ D_n= \begin{cases} nD_{n-1}+1, & n\text{ is even},\\ nD_{n-1}-1, & n\text{ is odd}. \end{cases} $$$

Exactly $$$k$$$ fixed points

Now suppose we no longer want zero fixed points. Instead, we want exactly $$$k$$$ of them.

First choose the $$$k$$$ positions which remain fixed. There are

$$$ \binom nk $$$

ways to do this.

After fixing these positions, none of the remaining $$$n-k$$$ positions may be fixed, otherwise the permutation would contain more than $$$k$$$ fixed points.

So the remaining part has to be a derangement, which gives

$$$ \binom nkD_{n-k}. $$$

These numbers are sometimes called Rencontres numbers.

A simple identity appears

Every permutation has some number of fixed points, from $$$0$$$ to $$$n$$$.

If we group all permutations according to that number, we get

$$$ \sum_{k=0}^{n} \binom nkD_{n-k} = n!. $$$
Why?

Replacing $$$n-k$$$ by $$$j$$$ gives another form of the same identity:

$$$ n! = \sum_{j=0}^{n} \binom njD_j. $$$

There is also a nice connection between this identity and the Inclusion-Exclusion formula.

Binomial inversion

Counting moved positions instead

Sometimes the statement never mentions fixed points directly and instead talks about positions satisfying

$$$ p_i\neq i. $$$

Suppose exactly $$$k$$$ positions move.

First choose these positions in $$$\binom nk$$$ ways. Inside the chosen positions, every element has to move, so they must form a derangement of size $$$k$$$.

Therefore the number of permutations with exactly $$$k$$$ moved positions is

$$$ \binom nkD_k. $$$

It follows immediately that the number of permutations with at most $$$k$$$ moved positions is

$$$ \sum_{i=0}^{k}\binom niD_i. $$$

Observation. Since $$$D_1=0$$$, a permutation cannot have exactly one moved position. If one value leaves its position, another value necessarily has to move as well.

Example: 888D - Almost Identity Permutations

The problem asks for permutations having at least $$$n-k$$$ fixed positions.

It is easier to reverse the condition:

$$$ \text{number of moved positions}\le k. $$$

Now the previous formula applies directly.

Solution

What if only some fixed points are forbidden?

The ordinary derangement problem is very symmetric: every index $$$i$$$ has exactly one forbidden assignment,

$$$ p_i=i. $$$

Many permutation problems break this symmetry.

Suppose there are $$$r$$$ remaining positions and $$$r$$$ remaining values, but only $$$m$$$ assignments can create forbidden fixed points.

For example, position $$$3$$$ may be free while value $$$3$$$ has already been used somewhere else. In that situation $$$p_3=3$$$ is impossible anyway, so position $$$3$$$ should not be considered a bad event.

Assume there are exactly $$$m$$$ dangerous fixed assignments.

Derivation

The ordinary derangement problem is just the special case

$$$ r=m=n. $$$

Substituting these values gives

$$$ \sum_{j=0}^{n} (-1)^j \binom nj (n-j)!, $$$

which is exactly $$$D_n$$$.

Example: 340E - Iahub and Permutations

We are given a partially known permutation. Some entries already contain values and the remaining positions contain -1.

The goal is to restore the permutation without creating fixed points.

The important question is not only how many positions are free, but also which free positions can actually become fixed points.

Getting to the formula

This example shows why thinking only in terms of the recurrence

$$$ D_n=(n-1)(D_{n-1}+D_{n-2}) $$$

can be too restrictive.

The recurrence works nicely when every position behaves in the same way. Once that symmetry disappears, the Inclusion-Exclusion interpretation is much more flexible.

Forbidden positions in general

The previous problems still have a special structure because every forbidden assignment has the form

$$$ p_i=i. $$$

Now suppose arbitrary assignments are forbidden, for example

$$$ p_2\neq5,\qquad p_4\neq1,\qquad p_6\neq3. $$$

A useful way to visualize this is with an $$$n\times n$$$ board.

Row $$$i$$$ represents position $$$i$$$, column $$$j$$$ represents value $$$j$$$, and cell $$$(i,j)$$$ represents the assignment

$$$ p_i=j. $$$

Mark every forbidden cell.

A permutation is exactly a choice of $$$n$$$ cells with one chosen cell in every row and every column.

So the problem becomes: how many such choices avoid all marked cells?

Rook numbers

Definition. Let $$$r_k$$$ be the number of ways to choose $$$k$$$ forbidden cells such that no two chosen cells share a row or a column.

Then the number of valid permutations is

$$$ \sum_{k=0}^{n} (-1)^k r_k (n-k)!. $$$
Why?

Derangements as a special rook problem

For an ordinary derangement, the forbidden cells are exactly the diagonal cells

$$$ (1,1),(2,2),\ldots,(n,n). $$$

Any $$$k$$$ diagonal cells automatically use different rows and columns, so

$$$ r_k=\binom nk. $$$

Substituting this into the general rook formula gives

$$$ \sum_{k=0}^{n} (-1)^k \binom nk (n-k)!, $$$

which is exactly the derangement formula.

The partial-permutation situation from 340E - Iahub and Permutations behaves similarly. There are only $$$m$$$ relevant forbidden diagonal cells, so

$$$ r_k=\binom mk, $$$

and the general formula becomes

$$$ \sum_{k=0}^{m} (-1)^k \binom mk (r-k)!. $$$

So these are not separate tricks. They are different forms of the same Inclusion-Exclusion argument.

A cycle decomposition view

Every permutation can be decomposed uniquely into disjoint cycles.

For example,

$$$ [2,1,4,5,3] $$$

can be written as

$$$ (1\ 2)(3\ 4\ 5). $$$

A fixed point is simply a cycle of length $$$1$$$, so a derangement is exactly a permutation whose cycle decomposition contains no $$$1$$$-cycles.

This gives another way to count $$$D_n$$$.

Suppose the cycle containing $$$1$$$ has length $$$k$$$. Since the permutation is a derangement,

$$$ k\ge2. $$$
Counting by the cycle containing 1

This recurrence is not usually the one we implement, but it makes the cycle structure behind derangements explicit.

Generating functions

This part is not necessary for solving the standard contest problems, but it gives a compact explanation of several formulas above.

Exponential generating function

Why does $$$e$$$ appear?

Compare the derangement formula

$$$ \frac{D_n}{n!} = \sum_{k=0}^{n}\frac{(-1)^k}{k!} $$$

with the Taylor expansion

$$$ e^{-1} = \sum_{k=0}^{\infty} \frac{(-1)^k}{k!}. $$$

Taking $$$n$$$ to infinity gives

$$$ \lim_{n\rightarrow\infty} \frac{D_n}{n!} = \frac1e. $$$

So the probability that a uniformly random permutation is a derangement approaches

$$$ \frac1e\approx0.367879. $$$

There is actually a stronger statement:

$$$ D_n = \left\lfloor \frac{n!}{e}+\frac12 \right\rfloor. $$$
Proof

This identity is nice mathematically, but using floating point is usually not the best way to compute derangements in a programming contest. The recurrence is exact and much safer.

How many fixed points does a random permutation have?

Let $$$X$$$ be the number of fixed points in a uniformly random permutation.

A slightly surprising fact is

$$$ E[X]=1 $$$

for every $$$n$$$, not only asymptotically.

Proof

Distribution of the number of fixed points

We already know that the number of permutations with exactly $$$k$$$ fixed points is

$$$ \binom nkD_{n-k}. $$$

Therefore

$$$ P(X=k) = \frac{\binom nkD_{n-k}}{n!}. $$$

After simplifying,

$$$ P(X=k) = \frac1{k!} \frac{D_{n-k}}{(n-k)!}. $$$

For fixed $$$k$$$, as $$$n$$$ grows,

$$$ \frac{D_{n-k}}{(n-k)!} \rightarrow \frac1e. $$$

Hence

$$$ P(X=k) \rightarrow \frac{e^{-1}}{k!}. $$$

This is exactly the probability mass function of a Poisson random variable with parameter $$$1$$$.

So the number of fixed points in a large random permutation approaches

$$$ \operatorname{Poisson}(1). $$$

The case $$$k=0$$$ gives the familiar probability $$$1/e$$$.

Implementation

For combinatorics problems, I usually keep factorials, inverse factorials and derangements in the same precomputation.

The recurrence

$$$ D_n=(n-1)(D_{n-1}+D_{n-2}) $$$

fits directly into the same template.

Implementation

After calling init(), the array stores

$$$ d[n]=D_n\pmod{MOD}. $$$

The same precomputation also gives

$$$ ncr(n,r)=\binom nr\pmod{MOD}. $$$

For example, the number of permutations with exactly $$$k$$$ fixed points modulo MOD is

1LL * ncr(n, k) * d[n - k] % MOD;

while the number of permutations with exactly $$$k$$$ moved positions is

1LL * ncr(n, k) * d[k] % MOD;

The only thing to keep in mind is that this template computes everything modulo MOD. If a problem asks for the exact answer, such as 888D - Almost Identity Permutations, ordinary integer arithmetic should be used instead of the modular ncr().

A useful way to recognize these problems

The recurrence for $$$D_n$$$ is easy to memorize, but it is also the least reusable part of the topic.

The more useful pattern is the bad event

$$$ p_i=i. $$$

When some assignments are forbidden, the natural question is whether forcing a set of forbidden assignments makes the remaining objects easy to count.

For ordinary derangements, forcing $$$k$$$ bad assignments leaves

$$$ (n-k)! $$$

possibilities.

For partial permutations, it leaves

$$$ (r-k)! $$$

possibilities.

For arbitrary forbidden positions, the extra difficulty is deciding which sets of bad assignments can happen simultaneously. This is exactly where the rook numbers $$$r_k$$$ appear.

So the progression is really

$$$ \text{derangements} \longrightarrow \text{partial derangements} \longrightarrow \text{forbidden positions}. $$$

The formula changes, but the Inclusion-Exclusion idea does not.

Problems

888D - Almost Identity Permutations

A direct application of choosing the moved positions and deranging them.

Main idea

340E - Iahub and Permutations

A partial permutation where only some remaining assignments can become fixed points.

Main idea

347B - Fixed Points

This one is not a counting problem, but it is a good exercise in thinking about fixed points and how a swap changes them.

1741B - Funny Permutation

A constructive permutation problem involving the condition

$$$ p_i\neq i. $$$

It is useful for seeing the difference between counting derangements and constructing one.

The standard formula

$$$ D_n = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!} $$$

is worth knowing.

But if I had to keep only one thing from this topic, it would be the Inclusion-Exclusion interpretation.

A fixed point is a bad assignment.

Once that is clear, ordinary derangements, partial derangements and permutations with forbidden positions stop looking like unrelated formulas.

  • Vote: I like it
  • +3
  • Vote: I do not like it