#P2354. [NOI2014] 随机数生成器

[NOI2014] 随机数生成器

Description

Little H is studying randomized algorithms. Randomized algorithms often obtain randomness by calling random number generator functions (for example, random in Pascal and rand in C/C++). In fact, such functions are not truly “random”; they are usually computed by some algorithm.

For example, the following quadratic polynomial recurrence is a common algorithm:

Choose non-negative integers x0,a,b,c,dx_0, a, b, c, d as the random seed, and compute using the following recurrence.

For any i1i \ge 1, $x_i = (a \times x_{i-1}^2 + b \times x_{i-1} + c) \mod d$. In this way we can obtain a non-negative integer sequence of arbitrary length {xi},i1\{ x_i \}, i \ge 1, and generally we regard this sequence as random.

Using the random sequence {xi},i1\{ x_i \}, i \ge 1, we can generate a random permutation of 11 to KK, denoted {Ti},i=1K\{ T_i \}, i = 1 \ldots K, by the following algorithm:

  1. Initialize TT as the increasing sequence 11 to KK.
  2. Perform KK swaps. At the ii-th swap, exchange the values of TiT_i and T(ximodi)+1T_{(x_i \bmod i) + 1}.

In addition, on top of these KK swaps, Little H performs QQ extra swap operations. For the ii-th extra swap, Little H chooses two indices uiu_i and viv_i, and swaps the values of TuiT_{u_i} and TviT_{v_i}.

To test the practicality of this random permutation generation algorithm, Little H designs the following problem:

Little H has a board with NN rows and MM columns. First, following the above process, after N×M+QN \times M + Q swap operations, she generates a random permutation of 1N×M1 \sim N \times M, denoted {Ti},i=1N×M\{ T_i \}, i = 1 \ldots N \times M. Then she fills these N×MN \times M numbers into the board row by row and column by column in order: that is, the number filled in the cell at row ii, column jj is T(i1)×M+jT_{(i - 1) \times M + j}.

Next, Little H wants to start from the top-left corner of the board, i.e., the cell at row 11, column 11. Each move goes either right or down, and without leaving the board, she reaches the bottom-right corner, i.e., the cell at row NN, column MM.

Little H records the numbers on all visited cells, and sorts them in ascending order. Thus, for any valid path, Little H can obtain an increasing sequence of length N+M1N + M - 1, which we call the path sequence.

Little H wants to know what the lexicographically smallest path sequence she can obtain is.

Input Format

The first line contains 55 integers x0,a,b,c,dx_0, a, b, c, d, describing the random seed required by Little H’s random number generation algorithm.

The second line contains three integers N,M,QN, M, Q, indicating that Little H wants to generate a permutation of 11 to N×MN \times M to fill her NN-row MM-column board, and that after the initial N×MN \times M swap operations, she performs QQ extra swap operations.

The next QQ lines each contain two integers ui,viu_i, v_i, indicating that at the ii-th extra swap operation, the values of TuiT_{u_i} and TviT_{v_i} are swapped.

Output Format

Output one line containing N+M1N + M - 1 positive integers separated by spaces, representing the lexicographically smallest path sequence that can be obtained.

1 3 5 1 71 
3 4 3 
1 7 
9 9 
4 9 
1 2 6 8 9 12 
654321 209 111 23 70000001 
10 10 0 
1 3 7 10 14 15 16 21 23 30 44 52 55 70 72 88 94 95 97
123456 137 701 101 10000007 
20 20 0 
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 

Hint

For Sample 1, according to the input seeds, the first 1212 random numbers xix_i that Little H obtains are:

9 5 30 11 64 42 36 22 1 9 5 30

Based on these 1212 random numbers, after the initial 1212 swap operations, Little H obtains the permutation:

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

After the additional 33 swap operations, Little H obtains the final random permutation:

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

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

The optimal path passes through the numbers: 12-9-1-6-2-8.

Translated by ChatGPT 5