TCO Semifinals 1 Editorial

Share

The competitors of this round are ACRush, Egor, Errichto, Marcin_smu, mjhun, tourist, Um_nik, and Vasyl[alphacom]. The easy is a simple constructive problem with a tricky edge case when the number of rows or columns is small. The medium is also a constructive problem that is the reverse of a classic problem. Finally, the last is a counting problem that requires a few observations to get it to run fast enough.

Congratulations to the advancers:

tourist

Errichto

Um_nik

ACRush

Lilypads (misof):

For this problem, we are given a 2D grid, where each grid cell contains a lilypad. We have a frog that can jump in any of the four cardinal directions (i.e. up, down, left, right) arbitrarily far away. We are given the initial location of the frog, and we would like to find any sequence of jumps that visits all lilypads exactly once, given that the frog cannot jump in the same direction more than once.

There are many different approaches to this problem.

One way is that we can solve this row by row. We can solve a single row from any starting position by repeatedly jumping to the leftmost and the rightmost unvisited cell. (If we start at the left end, the first jump has to be to the rightmost cell.)

Then, we can do a jump up or down to any unvisited row to continue. However, this approach might be incorrect when we have exactly one column (since we may be doing multiple jumps in the same direction if we are not careful).

To get around this, one way is to solve for rows and columns independently. Two 1D solutions can be used to construct a 2D solution. For instance always solve your whole current row according to the row sequence (alternately using it forwards and backwards) and then jump to the next column according to the column sequence.

For example, refer to the following picture. We have two 1-d solutions in grey, and the combination of the two solutions in the middle. Arrows are alternating colors for clarity.

DominoTiling (Alex_2oo8):

You are given a number up to . You would like to construct a 2d board with a small number of rows so that the number of domino tilings in the board is exactly . Some of the grid cells can be blocked, but you need to make sure that the unblocked part remains connected.

There are many possible solutions. We will show two of them.

Our first solution is based on the observation that a 2×2 square has 2 tilings, and thus a connection of n independent 2×2 squares has 2^n tilings. Of course, our area has to be connected, but that’s really easy to accomplish. Below is a board with 2^5 tilings.

..................
..##..##..##..##..

Now that we can create a valid board for each power of two, all we need is an “add” operation to construct a board with any general number of tilings. We will show this operation in the next solution. Instead, we will produce a more compact version. First, convince yourself that the following board has 2^5 + 1 tilings, as the loop adds exactly one new tiling:

Our second solution will be based on the “add” operation we mentioned above and on another class of boards that have nice numbers of tilings. It is easy to show that for any n, the 2xn rectangle has F_{n+1} tilings, where F are the Fibonacci numbers. These grow exponentially. Below we show a pattern with 21 + 21 + 3 tilings. Note that the rectangles in the middle two rows have 21, 21, and 3 tilings.

This can be generalized to any collection of odd-length rectangles. Hence, given any n we can do the following: greedily write n as a sum of (not necessarily distinct) even-indexed Fibonacci numbers, take the corresponding rectangles, and add the pattern that forces you to tile exactly one of them. This produces a pattern with O(log^2(n)) columns, as the sum will have O(log(n)) Fibonacci numbers and each of them will produce a rectangle with O(log(n)) columns.

FillInTheGraph (lg5293):

In this problem, we are given a directed graph. We would like to color the nodes of this graph so the following conditions are satisfied
– Each node is a color between and .
– For each i between and , if there is any node with color , then there must be at least one node with color that has an edge pointing to some node with color .

Two colorings are different if there exists a node with a different color in both colorings. The task is to count the number of colorings.

Let’s come up with a dp. We will first select which nodes get the number 1, then the nodes with number 2, and so on. In order to count these, we will have a dp that has states. The states are (a mask of nodes that already have a number, a mask of nodes that received the last number). In order to solve a given state we can iterate through all subsets of unlabeled nodes, check if the last group has at least one edge to this subset, and update our dp table if so.

The above runs in because whenever we try to label some new nodes, each node is in one of four states (colored long ago / colored in previous step / being colored / being left uncolored).

To speed this up, we can speed up the step of iterating through all subsets of the free nodes.
Instead of summing over subsets of free nodes that have at least one edge from the last group, let’s instead subtract from the total the number of subsets of free nodes that have no edges to the last group. This simplifies the problem by a little bit, since any subset of bad nodes must necessarily be a submask of the complement of nodes adjacent to the last group. So, this reduces to maintaining a subset sum for a fixed mask of nodes that have been taken. Thus, this reduces the overall running time to .

Example code below. In the implementation below, I add the two masks in base 3 which doesn’t overflow in each digit. Otherwise, you might need to compress the submasks into an array to get it to run fast enough.