Matrix Operation
Time limit2sMemory limit512 MB
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 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) ()
- 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 . The next three integers, A, B, and C, are coefficients used to calculate the values in the initial matrix , and they are used as follows: , where r and c are the row and column indices, respectively . The last four integers, D, E, F, and G, are coefficients used to compute the final hash value mentioned in the next section . 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.