#P6090. Cube Filling Words
Cube Filling Words
Cube Filling Words
In this problem, you are given an integer (with ) and a list of valid words, each of length . You are asked to construct a cube of side length , which is composed of unit cubes. Then, remove all unit cubes that do not lie on an edge of the cube. (Recall that a cube has 12 edges; the only remaining unit cubes are those that lie exactly on these edges; note that the corner cubes are shared by three edges.)
After the removal, each remaining unit cube is filled with a letter. For each of the 12 edges of the cube, when you read off the letters along the edge in one of the two possible directions, you must obtain one of the given valid words. (It is enough that one direction yields a valid word.)
More formally, consider the 8 corner unit cubes as vertices of a cube and the 12 edges connecting them. For each edge connecting vertices and , assign an ordered word (which is either one of the words in the input or its reverse) so that the letter in the unit cube at (one endpoint) is and at is . Note that different edges share the corner cubes; hence, the letter assigned to a corner must be consistent with all edges incident to that vertex.
Let be the number of ways a word (or its reverse) in the given list can have its first letter equal to the letter corresponding to and its last letter equal to the letter corresponding to . Then the answer is the sum over all assignments of letters to the 4 even (or one part of the bipartition) vertices of the cube of the product
$$\prod_{\text{odd vertex } v} \Bigl(\sum_{x=0}^{25} \prod_{u\in N(v)} f(\ell(u), x)\Bigr), $$where is the set of its 3 neighbors and is the letter assigned to vertex . Two cubes differing by any rotation or reflection are considered different.
Your task is to compute the number of distinct cubes modulo .
inputFormat
The first line contains two integers and , where is the side length of the cube and is the number of valid words. Each of the following lines contains a word of length . It is guaranteed that and that all words consist of uppercase English letters.
outputFormat
Output a single integer – the number of distinct cubes (i.e. valid letter assignments on the edges) modulo .
sample
2 1
AA
1