## NOI '14 P5 - Random Number Generator

View as PDF

Points: 20 (partial)
Time limit: 2.0s
Memory limit: 256M

Problem type
##### 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 , , , , and as its seed values and uses the following recursive calculations to generate a random number.

For any ,

This way, a sequence of nonnegative integers of arbitrary length can be obtained. Typically, we can consider this sequence to be random. Using the sequence , we can use the following algorithm to produce , a random permutation of the numbers to .

1. Initialize to the sequence of integers from to .
2. Perform swaps on the sequence . The -th swap will swap the value of with the value of .

Outside of this base number of swaps, little H has made an additional swaps. For the -th additional swap, little H will choose two positions and and swap the values of and .

To check the effectiveness of the random permutation generator, little H designed the following problem:

Little H has an row by column grid. She initially follows the above process, producing a random permutation of the integers from to after swaps. Then these values are then placed back into the grid, row for row, column for column. That is, the cell at column of row in the original grid will now take on the value of .

Afterwards, little H wishes to start from the top-left corner of the grid (i.e. row , column ), each step moving either right or down under the precondition that she does not move outside of the grid, and reach the bottom-right corner (i.e. row , column ).

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 which we will call the path sequence. Little H wishes to know the lexicographically smallest path sequence that she can obtain.

#### Input Specification

Line 1 of input consists of five integers , , , , and , representing the seed values to little H's random number generator.
Line 2 of input consists of three integers , , and , indicating that little H generates a permutation from 1 to to fill her grid. Also, after little H performs her swaps, she will perform an additional swaps.
The final lines will each contain two integers and , indicating that the -th additional swap involves swapping and .

#### Output Specification

The output should consist of one line containing space-separated positive integers, representing the lexicographically smallest path sequence that little H can obtain.

#### 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 352 354 383 395

#### Explanation

For sample 1, according to the input seed values, the first 12 random numbers of are:
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.

 12 9 1 7 5 11 6 2 4 10 3 8

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

#### Warning

This problem's memory limit is 256MB. Please ensure that the total memory of the execution of your submitted source code does not exceed this limit.
A 32-bit integer (e.g. int from C/C++ and Longint from Pascal) is four bytes long. So if your program declares a size array of 32-bit integers, 4MB of memory will be used.

Problem translated to English by Alex.