Editorial for TLE '17 Contest 3 P5 - Hypercube Hotel
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
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 .
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 
 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  region for every cell, since the number of cells to visit is 
, 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  to be the sum of the noise levels of rooms whose last 
 digits differ from room 
 by at most one, and whose remaining digits match exactly.
By considering the three cases for the 
-th digit from the right, we have the recurrence
(this recurrence ignores edge cases) where  is the product of the last 
 quantities 
. This choice of 
 corresponds to the value of the digit position in question, when converted from the mixed-radix number system.
Since the 
-th states only depend on the 
-th states, the usual memory optimization can be used.
This algorithm is more intuitive when considered geometrically.
We make  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 
 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 .
Exercise: Generalize the algorithm so that corresponding digits of neighbouring rooms can differ by at most , while keeping the same time complexity.
Comments