Editorial for TLE '17 Contest 3 P5 - Hypercube Hotel


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Eliden

There are two main ways of interpreting the problem statement. The most literal interpretation from the problem text is the one where the rooms are numbered according to a mixed-radix number system. The other important interpretation is that the rooms are cells in a hyper-rectangular grid (as roughly implied by the name of the hotel) with dimensions A_1 \times \dots \times A_N. The definition of "neighbour" makes more sense in the context of the second interpretation, where the set of neighbours of a room consists of the cells in the surrounding 3 \times \dots \times 3 region, excluding the room itself. This second interpretation is very useful in solving the problem conceptually, although properties of the first interpretation are also relevant in making a fast and elegant implementation.

First, the property of a room not being a neighbour of itself is a bit of misdirection. We can pretend that a room really is a neighbour of itself, then subtract a stored value from each cell just before we output our answer.

Next, note that it is too costly to sum up the entire 3 \times \dots \times 3 region for every cell, since the number of cells to visit is 3^N, which grows rapidly. Instead, we will have to use an algorithm that finds the noise levels for all rooms at once. That way, we can avoid recomputing the same information multiple times.

Indeed, the solution has a dynamic programming flavour to it. Formally, define dp[i][j] to be the sum of the noise levels of rooms whose last i digits differ from room j by at most one, and whose remaining digits match exactly. By considering the three cases for the i-th digit from the right, we have the recurrence

\displaystyle dp[i][j]=dp[i-1][j-p]+dp[i-1][j]+dp[i-1][j+p]

(this recurrence ignores edge cases) where p is the product of the last i quantities A. This choice of p corresponds to the value of the digit position in question, when converted from the mixed-radix number system. Since the i-th states only depend on the (i-1)-th states, the usual memory optimization can be used.

This algorithm is more intuitive when considered geometrically. We make N passes through the hyper-grid, and in each phase, we update the number in each cell to represent something new. Originally, each cell just contains the sum of itself. Then, we make it contain the sum of a "line" of three cells. Then, combining three "lines" together, a "plane" of 9 cells. Then summing the adjacent planes together, a cube of 27 cells. This continues until all 3^N cells are accounted for.

Finally, the time limit set for this problem is strict, to prevent less sophisticated algorithms from passing. Thus, as much as it is nice to understand the algorithm conceptually, it is also required to design a fast implementation. To begin, it is ideal to store the values in a one-dimensional array, and index entries using the mixed-radix number system discussed. Of course, nine-dimensional arrays are an option if you are crazy brave enough. To help with constant optimization, keep in mind that compilers are your friends - structure your code in a way that encourages automatic vectorization. This often means keeping the bodies of the innermost loops - typically the main bottlenecks of program execution - solely consisting of basic arithmetic operations, and not if statements. Another relevant principle is locality of reference.

The time complexity is \mathcal O(N A_1 A_2 \cdots A_N).

Exercise: Generalize the algorithm so that corresponding digits of neighbouring rooms can differ by at most R, while keeping the same time complexity.


Comments

There are no comments at the moment.