Editorial for Yet Another Contest 2 P5 - Mirror Maze
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Let's solve the problem times, once for each starting room.
Define as the probability of Josh arriving at the -th room with a dizziness of , after traversing hallways. Then, the answer is .
We can easily calculate the dp values if we compute them in increasing order of the number of hallways traversed. After calculating , for all , we will increment by . Here, denotes the bitwise or operator.
Time complexity:
Subtask 2
Observe that the bitwise or operation is independent for each bit. If we define as the expected final dizziness starting from room , and we define as the probability of the -th bit being set in the final dizziness starting from room , then by the linearity of expectation, we have . We will now focus on computing the probabilities for each bit independently, and then combine our computations at the end using this formula.
Let us only consider the -th bit.
At any point in time, Josh's state can be represented by his current room, and whether his current dizziness has the -th set. This gives us states, which we can label from to . If Josh is in the -th room with the -th bit set, let us label it with the -th state. If Josh is in the -th room with the -th bit not set, let us label it with the -th state.
Define as the probability of being in the -th state after traversing hallways, starting from the -th state.
Let us define matrix such that is if it is possible to move from the -th state to the -state by traversing exactly one hallway, and otherwise. Then, we can see that:
Hence, we have:
Recall that and for . This means that the matrix to the left on the right hand side is actually just the identity matrix, so we can ignore it. Therefore, we can compute:
We can compute this quickly using binary exponentiation.
Then, we have , so we can also compute quickly for all . After computing our matrix exponentiation for each bit , we can combine our results to determine the final answer.
Time complexity: (with a bad constant factor)
Subtask 3
Observe that for any bit , once it is set, the final dizziness must always have bit set.
Therefore, we can reduce the number of states from to ; we can have states representing that Josh is in a particular room with the current bit unset, and another state representing that the current bit is already set.
We can generate a similar matrix and transitions as to before. The exact details are left as an exercise to the reader. As we essentially halve the number of rows and columns in our matrices, the constant factor improves by a factor of about .
Time complexity: (with a good constant factor)
Comments