In all subtasks, we solve the problem independently for each possible length of a word, and sum the answers.
To handle palindromes and words that are reverse of each other, we first add the reverse of each word to the dictionary and then remove duplicates. Then, we will fix a direction for each edge and we will ask that the word written along the edge in that direction has to be in the new wordlist. (This is clearly equivalent to the original problem, and it allows us to choose the directions of edges in any way that suits our approach.)
For each pair of letters
precompute the number of words that begin with
and end in
.
Subtask 1
We brute-force all combinations of letters in the vertices of the cube and use the precomputed values to find how many cubewords are there with these letters fixed. Complexity
.
Subtask 2
Label the vertices
through
with vertices
through
lying around the bottom face and vertex
directly above vertex
. We will assign letters to vertices in the increasing order of their ids. Start by assigning letters to vertices
through
and count the number of possibilities of assigning letters to the edges on the bottom face. We then try all possibilities of assigning a letter to vertex
, and account for the edge
. Note that all edges incident to vertex
are already processed, hence we may now drop the value of
from our state. More generally, after processing vertex
, we can forget the letter in vertex
.
This forms a dynamic programming solution with
states, where each state has
transitions, each of which can be performed in constant time.
Complexity
.
Subtask 3
When viewed as a graph, the cube is bipartite with both partitions of size
. Consider all possibilities of assigning the letters to one partition. We want to process each of the remaining vertices in
.
Precompute
as the number of triples of words such that they all begin with the same letter and the other end with
, respectively. This precomputation can be done naively in
.
Coming back to our problem, let the letters on one partition be
, respectively. Observe that the answer is
.
Total complexity is
.
Fun fact: Geometrically, this solution can be viewed as follows. A cube can be cut into five tetrahedrons: one that's completely inside and four such that each of them contains one corner of the cube (and thus three edges). In this solution, you brute force the letters in the corners of the inner tetrahedron, and once you fix those, each of the remaining tetrahedrons is independent of the other three.
Subtask 4
To reduce the complexity even further, note that a general cube can be rotated and mirrored in
different ways: choose bottom face (
ways), then front face (
ways) and finally left face (
ways).
Assume for a moment that the letters
on the vertices of the first partition are guaranteed to be distinct. Then there is a way of rotating and flipping the cube in such a way that
. Hence we can calculate the answer only for this case and multiply it by
.
However, the letters
are not necessarily distinct. To mitigate this, we simply consider all possibilities of the form
, and multiply the answer of each of them by the number of permutations of
. This reduces this step of the solution to
.
Precomputing the array
can also be optimised – we again consider only cases
and use the fact that
. The complexity of this step is thus reduced to
.
To summarise, we are able to reduce the complexity
-fold by considering symmetries.
Comments