Pixel Shuffle
Time limit2sMemory limit1024 MB
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 and a list of elementary transformations that together define a shuffle of images. It must compute the smallest number (with ) such that applying exactly times always reproduces the original image.
For example, if is a counter-clockwise rotation, then .

Input
The input consists of two lines.
The first line contains the number (, and is even). An image is stored as an pixel matrix , where is the row index and is the column index. The upper-left pixel is at row , column .
The second line is a non-empty list of at most 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 rotation, that is a clockwise rotation.
The list denotes the compound transform ; that is, is applied first and is applied last. For instance, bvsym rot- first performs a clockwise rotation and then a vertical symmetry on the lower half of the image.

Each elementary transform maps an image to an image as follows.
Figure 1: how each transform turns image into image .
Output
Output a single line containing the smallest number () such that is the identity. You may assume that for every test input.






