(note: actual in-contest limit was 3 seconds, a higher limit was picked here to allow more things to work)
There are robots and tapes. The -th robot works on tape . Each tape is divided into squares from left to right, and are numbered . Each square has three possible states: (1) the square has number 0 (2) the square has number 1 (3) the square is empty.
At any point, a robot must stand in one of the squares of a tape.
After setting the initial positions of the robots on the tapes, the
-th robot will execute a predetermined sequence of operations .
The operations consist of characters R
, 0
, 1
, *
.
R
means the robot will move one square right. If there are no squares to the right, the robot will explode.0
means if the square the robot is currently at is nonempty, the robot will change the number in the square to 0. Otherwise, the robot will not modify the current square.1
means if the square the robot is currently at is nonempty, the robot will change the number in the square to 1. Otherwise, the robot will not modify the current square.*
means if the square the robot is currently at is nonempty, the robot will change the number currently in the square into . Otherwise, the robot will not modify the current square.
The state of the -th tape may be represented by a string of length
. Each character may be either 0
, 1
or -
(an empty square)
representing the state of the corresponding square. The initial state of
the -th tape is called the input to robot , . The state of
the tape after the operations is called the output of robot , .
Notice if the robot explodes, the robot won't have any output.
It is easy to see if a square is empty, the robot will never modify the square, so robots have the following property: if on tape (which is the tape robot works on), all the squares are empty, then the robot will do nothing, and the output of the robot simply says all squares are empty.
Now given inputs to the robots and initial target outputs , we want to find a position such that all robots can use the -th square of the tape as the start position, finish all operations without exploding, and satisfy the condition that the -th robot has output .
To make the problem harder, we want to know the number of combinations of inputs and outputs that have a feasible solution. In other words, we are interested in how many ways we may set the inputs to the robots and the target outputs such that there exists at least one position such that all robots can set the -th square of the tape as the start position, finish all operations without exploding, and the output of the -th robot is . Since the answer might be large, output the answer modulo . Two combinations are different if and only if there exists a robot such that its input or its output are different in the two combinations.
Input Specification
The first line contains two integers denoting the number of
squares on each tape and the number of tapes. In the following
lines, the -th line contains a string consisting of characters
R 0 1 *
, denoting the sequence of operations of robot .
Output Specification
The output consists of only one integer denoting the answer modulo .
Sample Input 1
2 1
1R*
Sample Output 1
9
Explanation for Sample Output 1
The possible combinations are given below:
No. | Feasible | ||
---|---|---|---|
1 | -- | -- | |
2 | 0- | 1- | |
3 | 1- | 1- | |
4 | -0 | -1 | |
5 | -1 | -0 | |
6 | 00 | 11 | |
7 | 10 | 11 | |
8 | 01 | 10 | |
9 | 11 | 10 |
Sample Input 2
3 2
1R0
*
Sample Output 2
1468
Explanation of Sample Output 2
We may compute the answer to this sample case using the inclusion-exclusion principle.
When is a feasible solution (i.e. after executing all the operations, all constraints are satisfied), then for square 0 of the first robot, either both the input and the output are empty, or the target output is 1. There are 3 possibilities. For square 1, either both the input and the output are empty, or the target output is 0, and there are 3 possibilities. Finally, for square 2, either both the input and the output are empty, or the input and the output are identical (since no operations are done for this square), and there are 3 possibilities. There are 27 possibilities for the first robot. For the second robot, for square 0 either both the input and the output are empty or the input and the target output are different. There are three possibilities. There are also 3 possibilities for square 1 and 2, so there are 27 possibilities for the second robot. In total, there shall be combinations.
When is a feasible solution, then for each of the three squares of the first tape (i.e. the tape of the first robot), there are three possibilities: for square 0 either the input and the output are empty or they are identical; for square 1 either both the input and the output are empty or the target output is 1; for square 2 either both the input and the output are empty or the output is 0. There are 27 possibilities for the first robot. For the second robot, for square 1 either both the input and the output are empty or they are different, so there are 3 possibilities. For square 0 and 2, either both the input and the output are empty, or the input and the output are identical, so there are 3 possibilities for each of the two squares. In total, there are combinations.
When is a feasible solution, then for the first robot, the inputs and the outputs are both empty (otherwise the first robot will explode). For the second robot, there are 27 possibilities.
If and are both feasible, then square 1 of the first robot must satisfy both the input and the output are empty. For square 0 of the first robot, either both the input and the output are empty or they are both 0. For square 2, either both the input and the output are empty or they are both 0. There are 4 possibilities for the first robot. For the second robot, square 0 and square 1 must be empty. There are 3 possibilities for square 2: either both the input and the output are empty or the input and the output are identical. There are 12 possibilities here.
If and are both feasible, then the inputs and the outputs of the first robot must be empty. Otherwise, the first robot will explode. For the second robot, square 0 and square 2 must be empty while there are 3 possibilities for square 1. There are 3 possibilities total.
If and are both feasible, then the inputs and the outputs of the first robot must be empty. For the second robot, square 1 and 2 must be empty, while there are 3 possibilities for square 0. There are 3 possibilities in total.
If are all feasible, then the inputs and the outputs of the two robots must be empty. There is only 1 possibility.
Therefore, by the inclusion-exclusion principle, the final answer is .
Additional Samples
Additional samples can be found here.
Constraints
For all test cases, , , .
Test case | Additional Constraints | ||
---|---|---|---|
1~2 | None. | ||
3 | |||
4 | |||
5~6 | |||
7 | |||
8~10 | |||
11~12 | |||
13~15 | There are no R operations. | ||
16~21 | For each sequence of operations, there are at most 15 R s. | ||
22~25 | None. |
Comments