Garbling Game
Time limit1sMemory limit128 MB
Simulate a periodic matrix-rotation process driven by a garbling map for up to 10^100 turns and count how many times each value is written, modulo 10^5.
- Level
Hard8 of 10
- Topics
- Simulation, Matrix, Math
- Solved
- No attempts yet
Problem
Pavel invented a game played on a matrix of integers. He starts with an matrix ( rows, columns) filled with the numbers through from left to right and top to bottom: sits in the upper-left corner and in the lower-right corner. Thus the cell in row and column (both -indexed) initially holds the value .
He then repeatedly rearranges the matrix and writes a sequence of numbers on a separate sheet; he calls this the garbling of the matrix. The rearrangement is driven by a garbling map: an matrix whose entries are the letters L, R, and N.
Pavel plays in a series of turns. The turns visit, in row-major order, every cell that can be the upper-left corner of a block — that is, every cell except those in the last row or the last column: . After the -th turn he returns to cell and continues in the same order, so the visited cells repeat with period .
On each turn Pavel first writes down the number currently stored in the visited cell. He then reads the letter of the garbling map at that same position and rearranges the block whose upper-left corner is the visited cell:
R— the block is rotated one quarter-turn clockwise.L— the block is rotated one quarter-turn counterclockwise.N— the block is left unchanged.
For a block
a b
c d
a clockwise rotation makes it
c a
d b
and a counterclockwise rotation makes it
b d
a c
As an illustration, with the initial matrix and the garbling map
L R L R
N L L R
L N N L
the first six numbers Pavel writes are (on the fifth turn the visited cell holds and the map letter is N, so nothing moves).
Given the garbling map and the number of turns Pavel makes, determine how many times each number is written down. Because the counts can be large, output each of them modulo .
Input
The first line contains three integers , , and , where and () are the dimensions of the initial matrix and () is the number of turns Pavel makes.
Each of the next lines contains characters, each R, L, or N, giving one row of the garbling map.
Output
Print lines. On the -th line print the number of times the value is written down during Pavel's turns, taken modulo .