Pixel Shuffle

Time limit2sMemory limit1024 MB

Summary
Given a permutation of an n by n pixel grid built from at most 32 named transformations, find the smallest positive power that returns the image to its original state.
Level

Medium7 of 10

Topics
Math, Implementation, Simulation, Number theory
Solved
No attempts yet

Problem

Shuffling the pixels of a bitmap image can produce a random-looking picture. Yet if you repeat the same shuffle enough times, the original image eventually reappears. This is no surprise: a "shuffle" is a one-to-one mapping (a permutation) of the finitely many cells of the image, so iterating it must return to the start.

Your program reads a number nn and a list of elementary transformations that together define a shuffle φ\varphi of n×nn \times n images. It must compute the smallest number mm (with m>0m > 0) such that applying φ\varphi exactly mm times always reproduces the original n×nn \times n image.

For example, if φ\varphi is a counter-clockwise 90∘90^\circ rotation, then m=4m = 4.

Input

The input consists of two lines.

The first line contains the number nn (2≤n≤10242 \le n \le 1024, and nn is even). An image is stored as an n×nn \times n pixel matrix (ai,j)(a_{i,j}), where ii is the row index and jj is the column index. The upper-left pixel is at row 00, column 00.

The second line is a non-empty list of at most 3232 words separated by spaces. A valid word is one of the keywords id, rot, sym, bhsym, bvsym, div, mix, optionally followed by a -. Each keyword key denotes an elementary transform (defined in Figure 1 below), and key- denotes the inverse of key. For instance, rot- is the inverse of the counter-clockwise 90∘90^\circ rotation, that is a clockwise 90∘90^\circ rotation.

The list k1,k2,…,kpk_1, k_2, \ldots, k_p denotes the compound transform φ=k1∘k2∘⋯∘kp\varphi = k_1 \circ k_2 \circ \cdots \circ k_p; that is, kpk_p is applied first and k1k_1 is applied last. For instance, bvsym rot- first performs a clockwise 90∘90^\circ rotation and then a vertical symmetry on the lower half of the image.

Each elementary transform maps an image (ai,j)(a_{i,j}) to an image (bi,j)(b_{i,j}) as follows.

TransformIllustration
id — identity. Nothing changes: bi,j=ai,jb_{i,j} = a_{i,j}.
rot — counter-clockwise 90∘90^\circ rotation: bi,j=aj,  n−1−ib_{i,j} = a_{j,\; n-1-i}.
sym — horizontal symmetry: bi,j=ai,  n−1−jb_{i,j} = a_{i,\; n-1-j}.
bhsym — horizontal symmetry applied to the lower half: if i≥n/2i \ge n/2 then bi,j=ai,  n−1−jb_{i,j} = a_{i,\; n-1-j}, otherwise bi,j=ai,jb_{i,j} = a_{i,j}.
bvsym — vertical symmetry applied to the lower half: if i≥n/2i \ge n/2 then bi,j=a 3n/2−1−i,  jb_{i,j} = a_{\,3n/2 - 1 - i,\; j}, otherwise bi,j=ai,jb_{i,j} = a_{i,j}.
div — division. Rows 0,2,…,n−20, 2, \ldots, n-2 become rows 0,1,…,n/2−10, 1, \ldots, n/2 - 1, while rows 1,3,…,n−11, 3, \ldots, n-1 become rows n/2,n/2+1,…,n−1n/2, n/2 + 1, \ldots, n-1.
mix — row mix. Rows 2k2k and 2k+12k+1 are interleaved. Row 2k2k of the new image is a2k,0,a2k+1,0,a2k,1,a2k+1,1,…,a2k, n/2−1,a2k+1, n/2−1a_{2k,0}, a_{2k+1,0}, a_{2k,1}, a_{2k+1,1}, \ldots, a_{2k,\,n/2-1}, a_{2k+1,\,n/2-1}, while row 2k+12k+1 of the new image is a2k, n/2,a2k+1, n/2,a2k, n/2+1,a2k+1, n/2+1,…,a2k, n−1,a2k+1, n−1a_{2k,\,n/2}, a_{2k+1,\,n/2}, a_{2k,\,n/2+1}, a_{2k+1,\,n/2+1}, \ldots, a_{2k,\,n-1}, a_{2k+1,\,n-1}.

Figure 1: how each transform turns image (ai,j)(a_{i,j}) into image (bi,j)(b_{i,j}).

Output

Output a single line containing the smallest number mm (m>0m > 0) such that φm\varphi^m is the identity. You may assume that m<231m < 2^{31} for every test input.

Examples2

  1. Example 1

    Input
    256
    rot- div rot div
    
    Expected output
    8
    
  2. Example 2

    Input
    256
    bvsym div mix
    
    Expected output
    63457