Inclusion-Exclusion by Mirror Pairs of Rows¶
Codex (GPT-6): application of inclusion-exclusion, grouping row sets by mirror relationships, with proof and a calculator. This method passes the full tests and independently regenerates the answer table.
Diagonals first¶
Let \(A_d\) count boards with \(d\) specified long diagonals forced black, while every row and column still contains white, for \(d=0,1,2\) . The two single-diagonal cases are symmetric, so the answer is
Inclusion-exclusion remains valid when the diagonals coincide for \(n=1\) . Return one separately for the empty board.
Exclude all-black rows¶
Choose a row set \(R\) to force black, with \(r=|R|\) . Let \(f_j\) be the number of cells in column \(j\) covered by neither these rows nor a forced diagonal. The column has \(2^{f_j}-1\) colorings containing white. Columns are independent, so the contribution is
Summing over all \(R\) gives \(A_d\) . Inclusion-exclusion cancels every board with an all-black row and retains every other board once.
For \(d=0\) , every column has \(n-r\) free cells:
For \(d=1\) , the diagonal cell is already covered in \(r\) columns. Each remaining column loses one additional free cell:
A factor with exponent zero is one without evaluating its base; this avoids evaluating a nonexistent negative free-cell count.
Force both diagonals black¶
Pair rows \(i,n+1-i\) , giving \(h=\lfloor n/2\rfloor\) mirror pairs. Suppose \(t,u,v\) pairs have zero, one, or two selected rows, respectively, with \(t+u+v=h\) . A one-row selection has two orientations, so the number of row sets in this category is
When a center row exists, let \(c\in\{0,1\}\) indicate whether it is selected; for even orders only \(c=0\) is allowed. There are \(r=u+2v+c\) selected rows. The three pair categories give \(2t,2u,2v\) columns with \(n-r-2,n-r-1,n-r\) free cells, respectively. The center column has only one distinct diagonal cell: it joins the second category if its row is unselected, and the third otherwise.
Thus a category contributes
Sum these contributions to obtain \(A_2\) . Each row set belongs to exactly one category, and all sets in a category have the same distribution of free-cell counts. Grouping therefore preserves the inclusion-exclusion sum.
Complexity and boundaries¶
One order takes \(O(n^2\log n)\) time. All orders through \(N\) take \(O(N^3\log N)\) time and \(O(N^2)\) space for binomial coefficients. Use 64-bit products with immediate modular reduction and normalize subtractions. Return one for zero exponents before inspecting a free-cell count; every count actually used as an array index is nonnegative.