Editorial for IOI '01 P3 - Twofive


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.

The solution is based on a function numconts, which, given an arbitrary set of letters having fixed positions, will compute the number of possible ways to legally place all the remaining letters. The function will try to position the letters in the edges of a shape of a Young tableau (the same shape that is used for the problem depot).

If all the letters have been placed, then there is exactly one way to continue: do nothing. So, for a full state, numconts initializes the table snum[states-1] to 1. Then, it will start to fill the table using the function calcstate.

Calcstate returns the number of possible ways to choose positions for the remaining letters, given the shape of the positioning of the earlier letters and the fixed set. It tries to place the next letter to all valid positions, and sums the values obtained by calling itself recursively for the new shapes. The intermediate results are stored in the table snum so that they don't have to be recalculated.

Solving the actual problem using the numconts function is fairly easy. Let's say we have to calculate the number of a word. We keep up a value corresponding to the number of the alphabetically first word we would be able to generate with our current the fixed set. We will fix all the letters one by one, starting from the most significant one. A letter is fixed by first setting it to A, and then incrementing it until the desired one is reached. Each time it is increased, numconts is used to calculate the number of words that were skipped in the operation, and the current value is updated accordingly. When all the letters have been fixed, we have the correct solution.

Doing things the other way around works almost the same way. We again fix the letters one by one, this time incrementing them until the current value exceeds the desired value. When this happens, we take a step back, and move to the next letter. When we're finished, the two values have become equal, and we have the correct word.


Comments

There are no comments at the moment.