luogu#P7740. [NOI2021] 机器人游戏

[NOI2021] 机器人游戏

Problem Description

Little R has mm (1m10001 \le m \le 1000) robots and mm paper tapes. The ii-th (1im1 \le i \le m) robot is responsible for operating on the ii-th tape. Each tape is divided from left to right into nn (1n321 \le n \le 32) cells, numbered 0,1,,n10, 1, \ldots, n - 1 in order. Each cell has 33 possible states: 1. the cell contains digit 00; 2. the cell contains digit 11; 3. the cell is empty.

At any moment, a robot must stand on a cell on its tape. After setting the robot’s initial position on the tape, the ii-th robot will execute a pre-defined operation sequence SiS_i in order. The operations are given by four characters: R, 0, 1, *, where:

  1. R means the robot moves one cell to the right. If there is no cell to the right, the robot explodes on the spot.
  2. 0 means: if the cell the robot is currently on is not empty, change the digit in that cell to 00; otherwise, do nothing.
  3. 1 means: if the cell the robot is currently on is not empty, change the digit in that cell to 11; otherwise, do nothing.
  4. * means: if the cell the robot is currently on is not empty, change the digit xx in that cell to 1x1 - x; otherwise, do nothing.

The state of the ii-th tape can be represented as a sequence of length nn, where each element is 0, 1, or - (empty), describing the state of each cell in order. The initial state of the ii-th tape is called robot ii’s input XiX_i, and the tape state after all operations are executed is called robot ii’s output YiY_i. Note that if a robot explodes, then it has no output.

You can see that if a cell is empty, the robot will never modify it. Therefore, each robot has the following property: if all cells on the tape of the ii-th robot are empty, then it will not execute any operations, and its output is that all cells are empty.

Now Little R provides each robot’s input XiX_i (i.e. the initial state of each tape) and the target output YiY_i. Little R wants Little D to find a position pp (0p<n0 \le p < n) such that all robots can start at cell pp on their own tapes, finish all operations without exploding, and satisfy that the output of robot ii is YiY_i.

Little D solved the problem in a few milliseconds. Now he wants to know: how many combinations of inputs and outputs make the above problem solvable? In other words, how many ways are there to set inputs X0,X1,,Xm1X_0, X_1, \ldots, X_{m - 1} and target outputs Y0,Y1,,Ym1Y_0, Y_1, \ldots, Y_{m - 1} for all robots such that there exists at least one position pp (0p<n0 \le p < n) where all robots can start from cell pp on their own tapes, finish all operations without exploding, and satisfy that the output of robot ii is YiY_i? Please help Little D solve this problem. Since the final answer may be very large, output the result modulo 109+7{10}^9 + 7.

Two combinations are different if and only if there exists at least one robot whose input or target output differs between the two combinations.

Input Format

The first line contains two positive integers n,mn, m, representing the number of cells on each tape and the number of tapes.

The next mm lines each contain a string SiS_i consisting only of the four characters R 0 1 *, representing the operation sequence of the ii-th robot.

Output Format

Output a single positive integer: the answer modulo 109+7{10}^9 + 7.

2 1
1R*

9

3 2
1R0
*

1468

见附件中的 robot/robot3.in
见附件中的 robot/robot3.ans
见附件中的 robot/robot4.in
见附件中的 robot/robot4.ans

Hint

[Sample Explanation #1]

Plan ID Input X0X_0 Target output Y0Y_0 Feasible starting positions pp
11 -- 0,10, 1
22 0- 1- 00
33 1-
44 -0 -1
55 -1 -0
66 00 11
77 10
88 01 10
99 11

In the table, - means an empty cell. Note that in plan 11, there are two empty cells in both the input and the output.

When the input is all empty, the starting position can be 00 or 11, because by the statement, when the input is all empty, the robot will not execute any operations.

When the input is not all empty, the starting position can only be 00. If the starting position is 11, the robot will definitely explode. So in this case, the actual executed operations are: change the digit in the first cell to 11, and change the digit xx in the second cell to 1x1 - x.

[Sample Explanation #2]

This sample can be computed using the inclusion-exclusion principle.

  1. Starting position p=0p = 0 can make the condition hold after all operations are executed. Then for the first robot, cell 00 is either empty in both input and output, or the target output is 11 (the input does not matter), so there are 33 possibilities. For cell 11, it is either empty in both input and output, or the target output is 00, also 33 possibilities. For cell 22, it is either empty in both input and output, or the input equals the target output (because no operation is performed on this cell), also 33 possibilities. So there are 2727 possibilities in total. For the second robot, cell 00 is either empty in both input and output, or the input differs from the target output, giving 33 possibilities; cells 11 and 22 also each have 33 possibilities, so there are 2727 possibilities in total. Therefore, there are 27×27=72927 \times 27 = 729 possibilities in total.
  2. Starting position p=1p = 1 can make the condition hold after all operations are executed. Then each of the three cells on the first robot’s tape has 33 possibilities: for cell 00, it is either empty in both input and output, or they are equal; for cell 11, it is either empty in both input and output, or the target output is 11; for cell 22, it is either empty in both input and output, or the output is 00. So there are 2727 possibilities. For the second robot, cell 11 is either empty in both input and output, or the input differs from the target output, giving 33 possibilities; for cells 00 and 22, they are either empty in both input and output, or the input equals the output, also 33 possibilities each, for a total of 2727 possibilities. Therefore, there are 27×27=72927 \times 27 = 729 possibilities in total.
  3. Starting position p=2p = 2 can make the condition hold after all operations are executed. Then the first robot’s tape must be all empty in both input and output (otherwise it explodes), so there is only 11 possibility. The second robot has 2727 possibilities, so there are 2727 possibilities in total.
  4. Both starting positions p=0,1p = 0, 1 satisfy the condition. This requires that for the first robot, cell 11 is empty in both input and output; for cell 00, input and output are either both empty or both 11; for cell 22, input and output are either both empty or both 00. So the first robot’s tape has 44 possibilities. For the second robot, cells 00 and 11 are empty, and cell 22 has 33 possibilities; equivalently, cells 00 and 11 must both be empty, and cell 22 is either empty in both input and output, or input equals output, giving 33 possibilities. There are 1212 possibilities in total.
  5. Both starting positions p=0,2p = 0, 2 satisfy the condition. Then the first robot’s tape must be all empty in both input and output (otherwise it explodes), so there is only 11 possibility. For the second robot, cells 00 and 22 are empty, and cell 11 has 33 possibilities. There are 33 possibilities in total.
  6. Both starting positions p=1,2p = 1, 2 satisfy the condition. Then the first robot’s tape must be all empty in both input and output, so there is only 11 possibility. For the second robot, cells 11 and 22 are empty, and cell 00 has 33 possibilities. There are 33 possibilities in total.
  7. Starting positions p=0,1,2p = 0, 1, 2 all satisfy the condition. Then the inputs and outputs of both robots must be all empty. There is 11 possibility in total.

By inclusion-exclusion, the final answer is 729+729+271233+1=1468729 + 729 + 27 - 12 - 3 - 3 + 1 = 1468.

[Constraints]

For all test points: 1n321 \le n \le 32, 1m10001 \le m \le 1000, 1Si1001 \le \lvert S_i \rvert \le 100.

Test point ID nn \le mm \le Special constraints
121 \sim 2 11 11 None
33 88
44 1616
565 \sim 6 3232
77 1616 55
8108 \sim 10 3232
111211 \sim 12 1616 10001000
131513 \sim 15 3232 A
162116 \sim 21 B
222522 \sim 25 None

Special constraint A: there is no R in the operation sequence.

Special constraint B: in each operation sequence, the number of R is at most 1515.

Translated by ChatGPT 5