number of rook placements avoiding~$H$:
$$N_0 = N(0) = \sum_{k=0}^n (-1)^k \cdot (n-k)! \cdot r_k.$$
-\example
+\example\id{hatcheck}%
Let us apply this theory to the hatcheck lady problem. The set~$H$ of holes is the main diagonal
of the board: $H=\{ (i,i) : i\in[n] \}$. When we want to place $k$~rooks on the holes,
we can do that in $r_k={n\choose k}$ ways. By the previous corollary, the number of
a~given order~$n$, we will call this matrix~$D_n$ and the graph~$G_n$ and
we will show that the submatrices of~$D_n$ share a~nice property:
-\lemma
+\lemma\id{submatrixlemma}%
If $D$ is a~submatrix of~$D_n$ obtained by deletion of rows and columns.
-Then the value of the permanent of~$D$ depends only on $n$ and the number of zero
-entries in~$D$.
+Then the value of the permanent of~$D$ depends only on the size of~$D$
+and the number of zero entries in it.
\proof
We know from Lemma~\ref{permchar} that the permanent counts matchings in the
As~$G_n$ is a~complete bipartite graph without edges of a~single matching,
the graph~$G$ must be also complete bipartite with some non-touching edges
-missing (but the number of such edges can be less than~$n$). Two such graphs
-$G$ and~$G'$ are therefore isomorphic if and only if the number of missing
-edges is the same. As the number of matchings is isomorphism invariant,
-the lemma follows.
+missing (but this matching is not necessarily perfect). Two such graphs
+$G$ and~$G'$ are therefore isomorphic if and only if they have the same
+number of vertices and also the same number of missing edges.
+As the number of matchings is isomorphism invariant, the lemma follows.
+\qed
+
+\defn
+Let $n_0(z,d)$ be the permanent shared by all submatrices as described
+by the above lemma, which have $d\times d$ entries and exactly~$z$ zeroes.
+
+\para
+Instead of maintaining the matrix~$M$ in the algorithm, it is sufficient to keep
+the number~$z$ of zeroes in the matrix and the set~$Z$ which contains the elements
+$x\in A$ whose locations are restricted (there is a~zero anywhere in the $(R_A(x)+1)$-th
+column of~$M$). In other words, every $x\in Z$ can appear at all positions in the
+permutation except one (and these forbidden positions are different for different~$x$'s),
+while the remaining elements of~$A$ can appear anywhere.
+
+As we already observed (\ref{hatcheck}) that the number of derangements on~$[n]$ is $\Theta(n!)$,
+we can again use word-sized vectors to represent the sets~$A$ and~$Z$ with insertion,
+deletion, ranking and unranking on them in constant time.
+
+The submatrix $M'=M^{1,k}$ for an~element $x$ of~rank~$k-1$ is described by either
+$z'=z-1$ and~$Z'=Z\setminus\{x\}$ (if $x\in Z$) or $z'=z$ and $Z'=Z'$ (if $x\not\in Z$).
+All computations of~$N_0$ in the algorithm can therefore be replaced by looking
+up the appropriate $n_0(z',\vert A\vert-1)$ in a~precomputed table. Moreover, we can
+calculate a~single~$C_a$ in constant time, because all summands are either $n_0(z,\vert A\vert-1)$
+or $n_0(z+1,\vert A\vert-1)$ depending on the set~$Z$. So we get:
+$$C_a = r\cdot n_0(z+1,\vert A\vert-1) + (a-r) \cdot n_0(z,\vert A\vert-1),$$
+where $r=R_Z(R^{-1}_A(a))$, that is the number of restricted elements among the $a$~smallest ones in~$A$.
+
+It remains to show how to precompute the table of the $n_0$'s efficiently.
+
+\lemma
+The function~$n_0$ satisfies the following recurrence:
+$$\eqalign{
+n_0(0,d) &= d!, \cr
+n_0(d,d) &= d! \cdot \sum\nolimits_{k=0}^d {(-1)^k \over k!}, \cr
+\noalign{\medskip}
+n_0(z,d) &= z\cdot n_0(z-1,d-1) + (d-z)\cdot n_0(z,d-1) \quad\hbox{for $z<d$.} \cr
+}$$
+
+\proof
+The base cases of the recurrence are simple: $n_0(0,d)$ counts the number of
+unrestricted permutations on~$[d]$ and $n_0(d,d)$ is equal to the number of derangements
+on~$[d]$, which we have already computed in Observation \ref{hatcheck}. Let us
+prove the third formula.
+
+We will count the permutations~$\pi$ restricted by a~matrix~$M$ of the given parameters
+$z$ and~$d$. As $z<d$, there is at least one position in the permutation for which
+no restriction applies and by Lemma~\ref{submatrixlemma} we can choose without
+loss of generality that it is the first position.
+
+If we select $\pi[1]$ from the~$z$ restricted elements, the rest of~$\pi$ is a~permutation
+on the remaining elements with one restriction less and there are $n_0(z-1,d-1)$ such
+permutations. On the other hand, if we use an~unrestricted element, all restrictions
+stay in effect, so there are~$n_0(z,d-1)$ ways how to do so.
\qed