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. $$$ DerivationSuppose we choose $$$k$$$ positions and force all of them to be fixed.
There are
$$$ \binom nk $$$ways to choose these positions.
Once they are fixed, the remaining $$$n-k$$$ elements can be permuted freely, giving
$$$ (n-k)! $$$possibilities.
Therefore the contribution of all intersections containing exactly $$$k$$$ bad events is
$$$ \binom nk(n-k)!. $$$Applying Inclusion-Exclusion,
$$$ \begin{aligned} D_n &=n!-\binom n1(n-1)!+\binom n2(n-2)!-\cdots+(-1)^n\\ &=\sum_{k=0}^{n}(-1)^k\binom nk(n-k)!. \end{aligned} $$$Now
$$$ \binom nk(n-k)! = \frac{n!}{k!}, $$$so the formula simplifies to
$$$ D_n = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}. $$$ 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} $$$ ProofSuppose first that
$$$ p_j=1. $$$Then $$$1$$$ and $$$j$$$ form a cycle of length $$$2$$$:
$$$ 1\rightarrow j\rightarrow1. $$$These two elements are completely determined. After removing them, the remaining $$$n-2$$$ elements still have to form a derangement.
This branch contributes
$$$ D_{n-2}. $$$Now suppose that
$$$ p_j\neq1. $$$Since every value appears exactly once, there is some $$$k$$$ such that
$$$ p_k=1. $$$Locally, the permutation contains
$$$ k\rightarrow1\rightarrow j. $$$Remove $$$1$$$ and connect $$$k$$$ directly to $$$j$$$:
$$$ k\rightarrow j. $$$The result is a derangement of the remaining $$$n-1$$$ elements.
This operation is reversible. Given such a derangement, find the element pointing to $$$j$$$ and insert $$$1$$$ between them.
So the two possibilities contribute
$$$ \begin{cases} D_{n-2}, & p_j=1,\\ D_{n-1}, & p_j\neq1. \end{cases} $$$For each of the $$$n-1$$$ possible values of $$$j$$$, there are therefore
$$$ D_{n-1}+D_{n-2} $$$constructions. Hence
$$$ D_n=(n-1)(D_{n-1}+D_{n-2}). $$$ 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. $$$ DerivationStart from
$$$ D_n = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}. $$$Separating the last term gives
$$$ D_n = n!\sum_{k=0}^{n-1}\frac{(-1)^k}{k!} + (-1)^n. $$$On the other hand,
$$$ D_{n-1} = (n-1)! \sum_{k=0}^{n-1}\frac{(-1)^k}{k!}. $$$Multiplying this identity by $$$n$$$ gives
$$$ nD_{n-1} = n! \sum_{k=0}^{n-1}\frac{(-1)^k}{k!}. $$$Substituting it in the previous expression,
$$$ D_n=nD_{n-1}+(-1)^n. $$$ 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?The term
$$$ \binom nkD_{n-k} $$$counts permutations with exactly $$$k$$$ fixed points.
Different values of $$$k$$$ describe disjoint groups, and every permutation belongs to exactly one of them.
Therefore their sum is the total number of permutations:
$$$ n!. $$$ 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 inversionStarting from
$$$ n! = \sum_{j=0}^{n} \binom njD_j, $$$binomial inversion gives
$$$ D_n = \sum_{j=0}^{n} (-1)^{n-j} \binom nj j!. $$$Letting $$$k=n-j$$$ transforms this into
$$$ D_n = \sum_{k=0}^{n} (-1)^k \binom nk (n-k)!, $$$which is exactly the Inclusion-Exclusion formula.
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.
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.
SolutionSuppose exactly $$$i$$$ positions move.
We first choose them:
$$$ \binom ni. $$$After that, every chosen position must contain a different value from its original one, so the chosen positions have to form a derangement.
This gives $$$D_i$$$ possibilities, so the number of permutations with exactly $$$i$$$ moved positions is $$$\binom niD_i$$$.
Summing over every allowed value of $$$i$$$,
$$$ \text{ans} = \sum_{i=0}^{k} \binom niD_i. $$$For example, when $$$k=2$$$,
$$$ \begin{aligned} \text{ans} &=\binom n0D_0+\binom n1D_1+\binom n2D_2\\ &=1+0+\binom n2\\ &=1+\binom n2. \end{aligned} $$$The missing case with exactly one moved position disappears automatically because $$$D_1=0$$$.
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.
DerivationIgnoring all restrictions, the $$$r$$$ remaining values can be placed in the $$$r$$$ free positions in
$$$ r! $$$ways.
If we force one dangerous fixed point, there are $$$(r-1)!$$$ ways to fill the rest. Since there are $$$m$$$ choices for this bad event, we subtract
$$$ \binom m1(r-1)!. $$$If we force two dangerous fixed points, there are $$$(r-2)!$$$ ways to fill the rest and $$$\binom m2$$$ ways to choose the two bad events, so this term is added back.
Continuing the Inclusion-Exclusion process gives
$$$ \begin{aligned} \text{ans} &=r!-\binom m1(r-1)!+\binom m2(r-2)!-\cdots\\ &=\sum_{j=0}^{m} (-1)^j \binom mj (r-j)!. \end{aligned} $$$ 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$$$.
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 formulaLet
$$$ F=\{i:a_i=-1\} $$$be the set of free positions, and let
$$$ M=\{x:x\text{ is missing from the permutation}\} $$$be the set of missing values.
Since every free position must eventually receive one missing value,
$$$ |F|=|M|. $$$Let
$$$ r=|F|=|M|. $$$A new fixed point at index $$$x$$$ is possible exactly when
$$$ x\in F\cap M. $$$Indeed, position $$$x$$$ has to be free and value $$$x$$$ has to be missing at the same time.
Let
$$$ m=|F\cap M|. $$$Now there are $$$r$$$ positions left to fill, but only $$$m$$$ assignments of the form $$$p_i=i$$$ are actually dangerous.
Applying Inclusion-Exclusion,
$$$ \begin{aligned} \text{ans} &=r!-\binom m1(r-1)!+\binom m2(r-2)!-\cdots\\ &=\sum_{j=0}^{m} (-1)^j \binom mj (r-j)!. \end{aligned} $$$ 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?In Inclusion-Exclusion, we choose some forbidden assignments and force all of them to occur.
However, not every set of forbidden assignments is compatible.
For example,
$$$ p_1=2 $$$and
$$$ p_1=5 $$$cannot both happen because they use the same position.
Similarly,
$$$ p_1=3 $$$and
$$$ p_4=3 $$$cannot both happen because a permutation uses each value only once.
Therefore a collection of bad assignments can occur simultaneously exactly when the corresponding cells use distinct rows and distinct columns.
This is precisely a placement of non-attacking rooks on the forbidden cells.
If we force $$$k$$$ compatible forbidden assignments, then $$$n-k$$$ positions and $$$n-k$$$ values remain, giving
$$$ (n-k)! $$$completions.
If $$$r_k$$$ counts the compatible choices of $$$k$$$ bad assignments, Inclusion-Exclusion gives
$$$ \sum_{k=0}^{n} (-1)^k r_k (n-k)!. $$$ 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 1Choose the other $$$k-1$$$ elements belonging to the same cycle as $$$1$$$:
$$$ \binom{n-1}{k-1}. $$$After choosing these elements, fix $$$1$$$ as the first element of the cycle. The other $$$k-1$$$ elements can appear in any order, giving
$$$ (k-1)! $$$possible cycles.
The remaining $$$n-k$$$ elements must form a derangement, giving $$$D_{n-k}$$$ possibilities.
Summing over the possible cycle lengths,
$$$ D_n = \sum_{k=2}^{n} \binom{n-1}{k-1} (k-1)! D_{n-k}. $$$ 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 functionA cycle of length $$$k$$$ contributes
$$$ \frac{x^k}{k} $$$to the exponential generating function for permutations.
Ordinary permutations allow cycles of every positive length, so their exponential generating function is
$$$ \exp\left( \sum_{k\ge1}\frac{x^k}{k} \right). $$$Using
$$$ \sum_{k\ge1}\frac{x^k}{k} = -\ln(1-x), $$$we get
$$$ \exp(-\ln(1-x)) = \frac1{1-x}. $$$For derangements, cycles of length $$$1$$$ are forbidden. Therefore the sum starts from $$$k=2$$$:
$$$ \begin{aligned} \sum_{n\ge0} D_n\frac{x^n}{n!} &= \exp\left( \sum_{k\ge2}\frac{x^k}{k} \right)\\ &= \exp\left(-\ln(1-x)-x\right)\\ &= \frac{e^{-x}}{1-x}. \end{aligned} $$$This identity also gives one of the previous recurrences.
Multiplying by $$$1-x$$$,
$$$ (1-x) \sum_{n\ge0} D_n\frac{x^n}{n!} = e^{-x}. $$$Since
$$$ e^{-x} = \sum_{n\ge0} \frac{(-1)^n x^n}{n!}, $$$comparing coefficients of $$$x^n$$$ gives
$$$ \frac{D_n}{n!} - \frac{D_{n-1}}{(n-1)!} = \frac{(-1)^n}{n!}. $$$Multiplying by $$$n!$$$,
$$$ D_n-nD_{n-1}=(-1)^n, $$$and therefore
$$$ D_n=nD_{n-1}+(-1)^n. $$$ 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. $$$ ProofThe series
$$$ \frac1e = 1-1+\frac1{2!}-\frac1{3!}+\frac1{4!}-\cdots $$$is alternating.
The error after stopping at the $$$n$$$-th term is smaller than the first omitted term:
$$$ \left| \frac1e- \sum_{k=0}^{n}\frac{(-1)^k}{k!} \right| \lt \frac1{(n+1)!}. $$$Multiplying by $$$n!$$$ gives
$$$ \left| \frac{n!}{e}-D_n \right| \lt \frac1{n+1}. $$$For $$$n\ge1$$$,
$$$ \frac1{n+1}\le\frac12. $$$Since $$$D_n$$$ is an integer, it must be the nearest integer to $$$n!/e$$$.
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.
ProofFor every position $$$i$$$, define
$$$ X_i= \begin{cases} 1, & p_i=i,\\ 0, & p_i\neq i. \end{cases} $$$Then
$$$ X=\sum_{i=1}^{n}X_i. $$$Every value is equally likely to occupy position $$$i$$$, so
$$$ E[X_i] = P(p_i=i) = \frac1n. $$$By linearity of expectation,
$$$ \begin{aligned} E[X] &=\sum_{i=1}^{n}E[X_i]\\ &=n\cdot\frac1n\\ &=1. \end{aligned} $$$Notice that independence was never needed.
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.
Implementationlong long power(long long a, long long b, long long MOD) {
long long res = 1;
while (b > 0) {
if (b & 1) {
res = (res * a) % MOD;
}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
const int N = 1e6 + 9;
const int MOD = 1e9 + 7;
int f[N], inv[N], finv[N], d[N];
void init() {
f[0] = 1;
for (int i = 1; i < N; i++) {
f[i] = 1LL * i * f[i - 1] % MOD;
}
inv[1] = 1;
for (int i = 2; i < N; i++) {
inv[i] = (-(1LL * MOD / i) * inv[MOD % i]) % MOD;
inv[i] = (inv[i] + MOD) % MOD;
}
finv[0] = 1;
for (int i = 1; i < N; i++) {
finv[i] = 1LL * inv[i] * finv[i - 1] % MOD;
}
d[0] = 1;
d[1] = 0;
for (int i = 2; i < N; ++i) {
d[i] = 1LL * (i - 1) * (d[i - 1] + d[i - 2]) % MOD;
}
}
int ncr(int n, int r) {
if (n < r || n < 0 || r < 0) {
return 0;
}
return 1LL * f[n] * finv[n - r] % MOD * finv[r] % MOD;
}
int npr(int n, int r) {
if (n < r || n < 0 || r < 0) {
return 0;
}
return 1LL * f[n] * finv[n - r] % MOD;
}
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
A direct application of choosing the moved positions and deranging them.
Main ideaIf exactly $$$i$$$ positions move, there are
$$$ \binom niD_i $$$such permutations.
Therefore
$$$ \text{ans} = \sum_{i=0}^{k} \binom niD_i. $$$ A partial permutation where only some remaining assignments can become fixed points.
Main ideaLet $$$r$$$ be the number of free positions and let $$$m$$$ be the number of indices which are both free positions and missing values.
Inclusion-Exclusion gives
$$$ \text{ans} = \sum_{j=0}^{m} (-1)^j \binom mj (r-j)!. $$$ This one is not a counting problem, but it is a good exercise in thinking about fixed points and how a swap changes them.
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.