Editorial for COCI '10 Contest 1 #6 Žabe


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.

We observe that the frog numbered N-1 reaches its initial position in a single jump. Similarly, the frog numbered N overshoots its initial position by one (effectively moving one position forward). The algorithm is as follows: we arrange frogs numbered 1 through N-1 in such a way that their relative order is preserved in the resulting arrangement. In the end, we just let the N^\text{th} frog leap until it reaches the correct position.

This leaves frogs numbered 1 through N-1, so we need to arrange them. Assume that frogs numbered N-1, N-2, \dots, X+1 are arranged into a correct relative ordering. Now, we try to add the X^\text{th} frog into the relative ordering without violating it. Among the frogs which have already been arranged, we find the frog which should immediately precede the frog numbered X. We denote by D the number of spaces the frog X needs to be moved to be correctly placed.

Let G be the greatest common divisor of X and N-1. Let us see how the frog numbered X leaps if the Frog Regent proclaims its name. After (N-1)/G proclamations, the frog will return to its initial position. After a proclamation, the value D \bmod G remains unchanged. Thus, if D \bmod G = 0, we can put the frog to the desired position by repeatedly proclaiming X.

In case D \bmod G \ne 0, we should change that number so that D \bmod G = 0 by gradually decreasing it as follows: we position the N^\text{th} frog immediately in front of X and then proclaim X once. Once D \bmod G reaches 0, we can put the frog to the desired position using the aforementioned procedure.

Alternative solution:

Let us assume that there exists a function which would decipher the proclamations needed to get the resulting arrangement of 1, 2, \dots, N. By changing the order of those proclamations, it is possible to get the reverse ordering, as well. This can be achieved by reversing the sequence of proclamations and replacing each proclamation with a series of same proclamations (left to the reader to calculate the exact number) which results in the reverse movement of the frog being moved.

The above function is yet to be realized. We will put the frog numbered N-2 immediately after the frog numbered N-1, then the frog numbered N-3 immediately after the frog numbered N-2 etc., with the frog numbered 1 being finally put immediately after the frog numbered 2. Then, the frog numbered N can trivially be put immediately after the first frog.

Assume that we wish to place the frog X immediately after X+1. We put the frog numbered 1 after the frog X+1, the frog 2 after X+1 or 1, the frog 3 after X+1 or 1 or 2 etc. We repeat this with all frogs up to and including X.

At that moment, we have a circle which comprises two parts: the first one is formed by frogs numbered 1 to X, and the second one by frogs numbered X+1 to N-1 (the position of the frog numbered N can be safely ignored).

Now we repeat the procedure, but this time without the frog numbered X. We put the frog numbered 1 after the frog X, the frog 2 after X or 1 etc. The final step is to put the frog numbered X-1 immediately after the frog X, 1, 2, \dots, or X-2. This results in the frog numbered X being put immediately after the frog numbered X+1, which was our initial intent.


Comments

There are no comments at the moment.