NOI '14 P5 - Random Number Generator

View as PDF

Submit solution

Points: 12 (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 x_0, a, b, c, and d as its seed values and uses the following recursive calculations to generate a random number.

For any i \ge 1, x_i = (a \times x^2_{i-1} + b \times x_{i-1}+c) \bmod d.

This way, a sequence of nonnegative integers \{x_i\}_{i \ge 1} of arbitrary length can be obtained. Typically, we can consider this sequence to be random. Using the sequence \{x_i\}_{i \ge 1}, we can use the following algorithm to produce \{T_i\}_{i=1}^K, a random permutation of the numbers 1 to K.

  1. Initialize T to the sequence of integers from 1 to K.
  2. Perform K swaps on the sequence T. The i-th swap will swap the value of T_i with the value of T_{(x_i \bmod i) + 1}.

Outside of this base number of K swaps, little H has made an additional Q swaps. For the i-th additional swap, little H will choose two positions u_i and v_i and swap the values of T_{u_i} and T_{v_i}.

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

Little H has an N row by M column grid. She initially follows the above process, producing a random permutation \{T_i\}_{i=1}^{N \times M} of the integers from 1 to N \times M after N \times M + Q swaps. Then these N \times M values are then placed back into the grid, row for row, column for column. That is, the cell at column j of row i in the original grid will now take on the value of T_{(i-1) \times M+j}.

Afterwards, little H wishes to start from the top-left corner of the grid (i.e. row 1, column 1), 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 N, column M).

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 N + M - 1 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 x_0, a, b, c, and d, representing the seed values to little H's random number generator.
Line 2 of input consists of three integers N, M, and Q, indicating that little H generates a permutation from 1 to N \times M to fill her N \times M grid. Also, after little H performs her N \times M swaps, she will perform an additional Q swaps.
The final Q lines will each contain two integers u_i and v_i, indicating that the i-th additional swap involves swapping T_{u_i} and T_{v_i}.

Output Specification

The output should consist of one line containing N + M - 1 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 347 352 354 383 395

Explanation

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

\displaystyle \begin{array}{|c|c|c|c|}\hline
12 & 9 & 1 & 7 \\\hline
5 & 11 & 6 & 2 \\\hline
4 & 10 & 3 & 8 \\\hline
\end{array}

The optimal path sequence is: 12 \to 9 \to 1 \to 6 \to 2 \to 8.

Constraints

The constraints of all the test cases are outlined below.

Test CaseN, MQOther Constraints
12 \le N, M \le 8Q = 0 0 \le a \le 300
0 \le b, c \le 10^8
0 \le x_0 < d \le 10^8
1 \le u_i, v_i \le N \times M
22 \le N, M \le 200
3
42 \le N, M \le 20000 \le Q \le 50\,000
5
6
72 \le N, M \le 5000
8
9
10

Problem translated to English by Alex.


Comments

There are no comments at the moment.