## NOI '21 P6 - Robot Game

View as PDF

Points: 25 (partial)
Time limit: 10.0s
Memory limit: 1G

Problem type

(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 -- -- 0,1
2 0- 1- 0
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.

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

6. 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.

7. If are all feasible, then the inputs and the outputs of the two robots must be empty. There is only 1 possibility.

Therefore, by inclusion-exclusion principle, the final answer is .

Additional samples can be found here.

#### Constraints

For all test cases, , , .

1~2 1 1 None.
3 8
4 16
5~6 32
7 16 5
8~10 32
11~12 16 1000
13~15 32 There are no R operations.
16~21 For each sequence of operations, there are at most 15 Rs.
22~25 None.