This page is still under construction.

Parts of this page are still being built. What you see may change.

Matrix Operation

Time limit2sMemory limit512 MB

Summary
Apply write, copy, row/column swap, rotate, and reflect operations to an N x N matrix, then hash a submatrix of the final result.
Level

Medium7 of 10

Topics
Implementation, Matrix, Simulation, Math
Solved
No attempts yet

Problem

You are a student looking for a job. Today you took an employment examination for an IT company. They asked you to write an efficient program that performs several operations. First, they showed you an N×NN \times N square matrix and a list of operations. All operations but one modify the matrix, and the last operation outputs the character in a specified cell. Remember that you need to output the final matrix after you finish all the operations.

Following are the details of the operations:

  • WR r c v: (Write operation) write an integer v into the cell (r,c) (1≤v≤1,000,0001 \leq v \leq 1,000,000)
  • CP r1 c1 r2 c2: (Copy operation) copy the character in the cell (r1,c1) into the cell (r2,c2)
  • SR r1 r2: (Swap Row operation) swap the r1-th row and r2-th row
  • SC c1 c2: (Swap Column operation) swap the c1-th column and c2-th column
  • RL: (Rotate Left operation) rotate the whole matrix counter-clockwise by 90 degrees
  • RR: (Rotate Right operation) rotate the whole matrix clockwise by 90 degrees
  • RH: (Reflect Horizontal operation) reverse the order of the rows
  • RV: (Reflect Vertical operation) reverse the order of the columns

Input

The first line of each testcase contains nine integers. The first two integers in the line, N and Q, indicate the size of the matrix and the number of queries, respectively (1≤N,Q≤40,000)(1 \leq N,Q \leq 40,000). The next three integers, A, B, and C, are coefficients used to calculate the values in the initial matrix (1≤A,B,C≤1,000,000)(1 \leq A,B,C \leq 1,000,000), and they are used as follows: Ar,c=(r∗A+c∗B) mod CA_{r,c} = (r * A + c * B) \bmod C, where r and c are the row and column indices, respectively (1≤r,c≤N)(1\leq r,c\leq N). The last four integers, D, E, F, and G, are coefficients used to compute the final hash value mentioned in the next section (1≤D≤E≤N,1≤F≤G≤N,E−D≤1,000,G−F≤1,000)(1 \leq D \leq E \leq N, 1 \leq F \leq G \leq N, E - D \leq 1,000, G - F \leq 1,000). Each of the next Q lines contains one operation in the format described above.

Output

Output the hash value h computed from the final matrix B using the following pseudocode.

h <- 314159265
for r = D...E
  for c = F...G
    h <- (31 * h + B_{r,c}) mod 1,000,000,007

Here, "<-" is a destructive assignment operator, "for i = S...T" indicates a loop over i from S to T (both inclusive), and "mod" is a remainder operation.

Examples4

  1. Example 1

    Input
    2 1 3 6 12 1 2 1 2
    WR 1 1 1
    
    Expected output
    676573821
    
  2. Example 2

    Input
    2 1 3 6 12 1 2 1 2
    RL
    
    Expected output
    676636559
    
  3. Example 3

    Input
    2 1 3 6 12 1 2 1 2
    RH
    
    Expected output
    676547189
    
  4. Example 4

    Input
    39989 6 999983 999979 999961 1 1000 1 1000
    SR 1 39989
    SC 1 39989
    RL
    RH
    RR
    RV
    
    Expected output
    458797120