National Olympiad in Informatics, China, 2014
Little H has recently been studying randomized algorithms. Randomized
algorithms often use random number generation functions (e.g. random
from Pascal and rand
from C/C++) to obtain their randomness. In
reality, random number functions are not truly "random." Instead, they
work off of some specific algorithms.
As such, the following recursive quadratic polynomial is one method:
The algorithm selects nonnegative integers
For any
This way, a sequence of nonnegative integers
- Initialize
to the sequence of integers from to . - Perform
swaps on the sequence . The -th swap will swap the value of with the value of .
Outside of this base number of
To check the effectiveness of the random permutation generator, little H designed the following problem:
Little H has an
Afterwards, little H wishes to start from the top-left corner of the
grid (i.e. row
Little H writes down the value of every cell she travels through,
ordered from least to greatest. This way, for any valid path, little
H can obtain an increasing sequence of length
Input Specification
Line 1 of input consists of five integers
Line 2 of input consists of three integers
The final
Output Specification
The output should consist of one line containing
Sample Input 1
1 3 5 1 71
3 4 3
1 7
9 9
4 9
Sample Output 1
1 2 6 8 9 12
Sample Input 2
654321 209 111 23 70000001
10 10 0
Sample Output 2
1 3 7 10 14 15 16 21 23 30 44 52 55 70 72 88 94 95 97
Sample Input 3
123456 137 701 101 10000007
20 20 0
Sample Output 3
1 10 12 14 16 26 32 38 44 46 61 81 84 101 126 128 135 140 152 156 201 206 237 242 243 253 259 269 278 279 291 298 338 345 347 352 354 383 395
Explanation
For sample 1, according to the input seed values, the first 12 random numbers of
9 5 30 11 64 42 36 22 1 9 5 30
With these 12 random numbers, little H will perform 12 swap operations, yielding the following:
6 9 1 4 5 11 12 2 7 10 3 8
After the additional 3 swap operations, little H obtains the final permuted sequence of:
12 9 1 7 5 11 6 2 4 10 3 8
This sequence will yield the following grid.
The optimal path sequence is:
Constraints
The constraints of all the test cases are outlined below.
Test Case | Other Constraints | ||
---|---|---|---|
1 |
| ||
2 | |||
3 | |||
4 | |||
5 | |||
6 | |||
7 | |||
8 | |||
9 | |||
10 |
Problem translated to English by .
Comments