# WARNING: The text below provides a guidance for a Project Euler problem.

• If you pride yourself in tackling problems entirely on your own, you are encouraged to exit this post right now.

• If you are simply looking for a hint for the problem, please visit the »Project Euler’s official forum«. You may find some hints here, but if you don’t want your problem spoiled, scroll cautiously.

• If you are looking for a readily available copy/paste answer, you will not find it here. All of the code snippets in the post have been stripped of some crucial lines (these are clearly marked). It is my aim that anyone able to replicate the answer based on such snippets will have to understand the solution conceptually. At that point, by virtue of learning something new, I believe he deserves it anyway.

• Guidance for future problems will not be published before 100 people have solved the problem and there are at least two more recent problems.



### Open chess positions

Yet another problem from the recent “counting” streak tasks us with computing the number $f(n)$ of configurations of $n$ pawns on an $n \times n$ board, such that the following conditions hold:

• (1) No two pawns share a row or column.
• (2) It is possible for a rook to get from the bottom-left corner to the top-right corner of the board.

How does a position that satisfies both of the above look like? It seems to be the easiest to work with the complement. We will characterise instead the positions where the pawns are positioned according to the first rule, but the rook has no way of getting from one corner to the other.

Lemma: Rook has no way between the bottom-left corner and top-right corner if and only if some of the board’s $2n - 1$ “subdiagonals” (as represented by different colors in the picture above) is completely obstructed.

Proof. If any of the subdiagonals is obstructed, the way between the corners clearly doesn’t exist. Suppose that none of the subdiagonals is completely obstructed. Consider the pawn $C$ that was placed somewhere in the first column. Without loss of generality, on our picture, this would be on the yellow subdiagonal. On the subdiagonal of this pawn (yellow), there is going to be a sequence of pawns going right-downward, running until the first free square (which exists by assumption). The key realization is then that any square in the same row as $C$ is necessarily free and reachable with the rook from the bottom-left corner.
With a similar logic, there must also be some pawn $R$ in the first row. If $R = C$, we have already found a way for rook to reach the other corner. Assume therefore that $R \neq C$. For this pawn, any square in the same column as $R$ is necessarily free and reachable with the rook from the top-right corner. But the column of $R$ and the row of $C$ must intersect, and thus the rook can safely move between the corners at his leisure.

Another handy observation we can make straight away is the fact that at most 2 subdiagonals of the board can be completely obstructed - one in the lower-left (triangular) region below the main diagonal, and one in the upper-right region above the main diagonal (For if there were two subdiagonals obstructed on either side, we would need to have more than 1 pawns in the same row/column). Let us get to counting now.

Let $t(n)$ be the total number of configurations of pawns that satisfy rule 1. Clearly, $t(n) = n!$ (When placing pawns row by row, the number of options after the $i$-th pawn was placed is $i$).
Let $s(n, l, r)$ be the total number of configurations of pawns that satisfy rule 1, where subdiagonal of length $l$ in the lower-left region below the main diagonal is completely obstructed, and subdiagonal of length $r$ in the upper-right region above the main diagonal is completely obstructed. In case no subdiagonal in either region is completely obstructed, we set $l = 0$ or $r = 0$ respectively. Then:

where $-1$ comes from the case where the main diagonal itself is obstructed.

By the same combinatorial argument that we used to find $t(n) = n!$, we can fix any $r > 0$ and determine:

and symetrically for any $l > 0$:

For any $l, r > 0$ we also have $% $.

By carefully treating the indices, we can also rewrite the required double sum:

where in the last step, in the second sum, we have just counted how many times $(n - 1 - i)!$ appears in $\sum_{r = 1}^{n-1} \sum_{l = 1}^{n-r} (n - r - l)!$. Altogether, we obtain a sum that can be computed in linear time:

The following piece of Python code gives the answer in below 5 seconds, when run with PyPy: