#NOI2014E. 随机数生成器 (Random Number Generator)

随机数生成器 (Random Number Generator)

Description

Hacker is studying randomized algorithms recently. Randomized algorithms usually achieve randomness by calling random number generating (RNG) functions (such as random in Pascal or rand in C/C++). RNG functions are actually pseudo-random, with numbers created by certain algorithms.

For example, the quadratic congruential generator as stated below is a common algorithm:

Given non-negative integers x0,a,b,c,dx_0,a,b,c,d, this algorithm calculates the recurrence as follows:

$$\forall i \geq 1, x_i = ( ax_{i-1}^2+bx_{i-1}+c ) \bmod d $$

Therefore a sequence {xi}i1\{x_i\}_{i \geq 1} of non-negative integers of arbitrary length could be generated. In general, we regard this sequence as random.

Using a random sequence {xi}i1\{x_i\}_{i \geq 1}, we could also produce a random permutation {Ti}i1K\{T_i\}_{i \geq 1}^K from 11 to KK according to the algorithm as follows:

  1. Initially set T as the increasing sequence of 1K1 \sim K;
  2. Perform KK swaps on TT, with the iith one exchanging the value of TiT_i and T(ximodi)+1T_{(x_i \bmod i)+1}.

Hacker performs QQ swaps in addition to the KK swaps. For the iith additional swap, Hacker chooses two additional indices uiu_i and viv_i and swaps the value of TuiT_{u_i} and TviT_{v_i}.

To put the usefulness of the RNG algorithm to test, Hacker designs a problem:

Hacker begins with a chessboard of NN rows and MM columns. She starts with the process above, generating a random permutation {Ti}i1N×M\{T_i\}_{i \geq 1}^{N \times M} of 1N×M1 \sim N \times M by N×M+QN \times M + Q swaps. Then the chessboard is filled by the N×MN \times M numbers rowwise: that is, the number in the iith row and jjth column is T(i1)M+jT_{(i-1)M+j}.

Then Hacker starts from the top-left corner of the board, namely the square in the first row and first column, and move right or down once at a time, without leaving the board, until reaching the bottom-right corner, or the square in the NNth row and MMth column.

Hacker records all numbers in passed squares and sorts them into increasing order. Therefore, for any valid route, Hacker could get a increasing sequence of length N+M1N+M-1 called sequence of route.

Hacker wants to know, what is the sequence of route with the smallest lexicographical order?

Input Format

The first line contains five integers, x0,a,b,c,dx_0,a,b,c,d in order, describing the random seed of Hacker's RNG algorithm.

The second line contains three integers N,M,QN,M,Q, indicating Hacker wants to generate a permutation of 1N×M1 \sim N \times M to fill in her chessboard with NN rows and MM columns and after the initial N×MN \times M swaps, Hacker performs an additional QQ swaps.

The iith line in the next QQ lines contains two integers ui,viu_i,v_i, indicating the iith additional swap exchanges the value of TuiT_{u_i} and TviT_{v_i}.

Output Format

The output consists of one line of N+M1N+M-1 integers separated by spaces, representing the sequence of route with the smallest lexicographical order.

1 3 5 1 71
3 4 3
1 7
9 9
4 9
1 2 6 8 9 12

Constraints and Hint

The memory limit of this problem is 256 MB256\ MB, so please make sure the total memory use of submitted code does not exceed this limit. A 3232-bit integer (such as int in C/C++ or Longint in Pascal) uses 44 Bytes, so declaring a 10241024 by 10241024 array of 3232-bit integers in the program will consume 4 MB4\ MB of memory.

For all the data, 2N,M50002 \leq N,M \leq 5000, 0Q500000 \leq Q \leq 50000, 0a3000 \leq a \leq 300, 0b,c1080 \leq b,c \leq 10^8, 0x0<d1080 \leq x_0 < d \leq 10^8, 1ui,viN×M1 \leq u_i,v_i \leq N \times M.