#P2354. Lexicographically Minimum Path Sequence

    ID: 15627 Type: Default 1000ms 256MiB

Lexicographically Minimum Path Sequence

Lexicographically Minimum Path Sequence

In this problem, you are given an N×M board that is filled with a permutation of the numbers from 1 to N×M. The board is generated as follows:

  1. Let K = N×M and let T be an array initially containing the ordered sequence 1, 2, …, K.
  2. A random sequence {(x_i)} for (i=1\ldots K) is generated by a quadratic recurrence with parameters (x_0, a, b, c, d) defined by [ x_i = (a\times x_{i-1}^2+b\times x_{i-1}+c) \bmod d, \quad \text{for } i \ge 1. ]
  3. For each (i=1) to (K) (using 1-indexing), swap (T_i) with (T_{(x_i \bmod i)+1}).
  4. Then, Q extra swap operations are performed. In the (i)th extra swap, two indices (u_i) and (v_i) are given and the values (T_{u_i}) and (T_{v_i}) are swapped.
  5. Finally, the board is filled row‐wise: the cell in row (i) and column (j) receives the number (T_{(i-1)\times M+j}).

After the board is generated, a path is defined as a sequence of moves from the top–left cell (1,1) to the bottom–right cell (N,M) where you can only move right or down. For any valid path, consider the multiset of numbers on the visited cells; after sorting these numbers in ascending order we obtain what is called a "path sequence" (of length (N+M-1)).

Your task is to choose a valid path so that its resulting (sorted) path sequence is lexicographically minimum among all valid paths. Two sequences (A) and (B) are compared lexicographically in the usual way: the first position where they differ determines the order.

It is guaranteed that (N, M, Q) and the random generator parameters are such that a solution can be computed within the resource limits.

All formulas are given in LaTeX format.

inputFormat

The input begins with three integers (N), (M) and (Q) on one line. The next line contains five non‐negative integers: (x_0), (a), (b), (c) and (d) which are used to generate the random sequence. Then (Q) lines follow, each containing two integers (u_i) and (v_i) (1-indexed) representing an extra swap operation on the permutation (T). (K = N \times M) is the length of the permutation.

outputFormat

Output the lexicographically minimum path sequence obtained by taking a valid path from the top–left to the bottom–right cell. Print the (N+M-1) numbers in one line, separated by a single space.

sample

2 2 0
1 1 1 1 10
1 3 4