Editorial for COI '19 #3 Semafor
Submitting an official solution before solving the problem yourself is a bannable offence.
We will solve the harder case .
We represent a state of the board by a bitmask of bytes. Call a bitmask nice if the represented board shows a valid number.
It is well known that the vertices of the hypercube graph are all bitmasks of some size ( here), and the edges connect bitmasks which differ in exactly one place. Let denote the adjacency matrix of the hypercube.
Then the problem, formally, asks for the following: for each nice bitmask , find the number of walks of length from to of length in the hypercube, such that the walk visits the set of nice bitmasks every steps.
Claim 1: The number of walks of length in the hypercube, starting in and ending in , equals the -th entry in the matrix .
Proof: Look into Daniel A. Spielman's "Spectral and Algebraic Graph Theory", brilliant stuff. For an elementary proof, it's Lemma 3 on this link. □
Definition 2: Let be the principal submatrix of indexed by only the rows and columns of the nice bitmasks.
Claim 3: The number of walks (from to ) of length in the hypercube, where divides , such that every steps the walk visits a nice bitmask, equals the -th entry in the matrix .
Proof: Left as an exercise. □
There are several steps in the solution. We need to calculate the matrix , then calculate , and then multiply it with the matrix corresponding to the last steps. We omit the third because it is very similar to the first step, so we assume divides in the rest of the editorial.
Let us solve the second step (raising to the -th power) first. It is enough to use naive binary exponentiation and multiply two matrices in the ordinary cubic complexity, because for we are dealing with matrices.
The first step is tougher, because is a submatrix of , and is a matrix for . Naive calculation of passes only the subtask . For smaller and , it can be done with clever dynamic programming, and this should give the first four subtasks.
For the full problem, we need to raise the hypercube matrix to some power faster.
Claim 4: The -th entry of the matrix depends only on the number of bits in which the bitmasks and differ.
Proof: Without loss of generality, we can XOR all vertices of the hypercube with some fixed bitmask. Thus, we can assume . It's also clear that the order of the bits doesn't matter at all. Hence, the number of walks from to depends only on the bitcount of .
Claim 4 shows that it's enough to calculate the first row of the matrix . Depending on the exact implementation, it can pass the first five subtasks, or even pass the whole problem. (Ask Dorijan Lendvaj for the XOR-convolution solution of quadratic complexity that runs faster than the official solution.)
The official solution calculates the numbers of walks starting in in a smarter way. It's enough to calculate, for every , the number of walks starting from bitcount and ending in bitcount .
The number of ways in which we can get from one bitcount to another is given by the following matrix:
By raising the matrix to a suitable power, we can get the number of walks of length from bitcount to each bitcount . To get the actual number of walks in the hypercube, we just need to divide by a suitable binomial coefficient, due to symmetry.
There is an even faster solution, which uses exponential generating functions. The number of walks of length from the bitmask to some bitmask with bits equals the coefficient next to of the following expression:
By expanding the expression carefully, we get a solution of the complexity , that is, calculating entries of the hypercube matrix raised to some power is possible in time .
For similar ideas, look into Herbert Wilf's free book generatingfunctionology.
Comments