luogu#P7740. [NOI2021] 机器人游戏
[NOI2021] 机器人游戏
Problem Description
Little R has () robots and paper tapes. The -th () robot is responsible for operating on the -th tape. Each tape is divided from left to right into () cells, numbered in order. Each cell has possible states: 1. the cell contains digit ; 2. the cell contains digit ; 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 -th robot will execute a pre-defined operation sequence in order. The operations are given by four characters: R, 0, 1, *, where:
Rmeans the robot moves one cell to the right. If there is no cell to the right, the robot explodes on the spot.0means: if the cell the robot is currently on is not empty, change the digit in that cell to ; otherwise, do nothing.1means: if the cell the robot is currently on is not empty, change the digit in that cell to ; otherwise, do nothing.*means: if the cell the robot is currently on is not empty, change the digit in that cell to ; otherwise, do nothing.
The state of the -th tape can be represented as a sequence of length , where each element is 0, 1, or - (empty), describing the state of each cell in order. The initial state of the -th tape is called robot ’s input , and the tape state after all operations are executed is called robot ’s output . 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 -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 (i.e. the initial state of each tape) and the target output . Little R wants Little D to find a position () such that all robots can start at cell on their own tapes, finish all operations without exploding, and satisfy that the output of robot is .
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 and target outputs for all robots such that there exists at least one position () where all robots can start from cell on their own tapes, finish all operations without exploding, and satisfy that the output of robot is ? Please help Little D solve this problem. Since the final answer may be very large, output the result modulo .
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 , representing the number of cells on each tape and the number of tapes.
The next lines each contain a string consisting only of the four characters R 0 1 *, representing the operation sequence of the -th robot.
Output Format
Output a single positive integer: the answer modulo .
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 | Target output | Feasible starting positions |
|---|---|---|---|
-- |
|||
0- |
1- |
||
1- |
|||
-0 |
-1 |
||
-1 |
-0 |
||
00 |
11 |
||
10 |
|||
01 |
10 |
||
11 |
|||
In the table, - means an empty cell. Note that in plan , there are two empty cells in both the input and the output.
When the input is all empty, the starting position can be or , 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 . If the starting position is , the robot will definitely explode. So in this case, the actual executed operations are: change the digit in the first cell to , and change the digit in the second cell to .
[Sample Explanation #2]
This sample can be computed using the inclusion-exclusion principle.
- Starting position can make the condition hold after all operations are executed. Then for the first robot, cell is either empty in both input and output, or the target output is (the input does not matter), so there are possibilities. For cell , it is either empty in both input and output, or the target output is , also possibilities. For cell , 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 possibilities. So there are possibilities in total. For the second robot, cell is either empty in both input and output, or the input differs from the target output, giving possibilities; cells and also each have possibilities, so there are possibilities in total. Therefore, there are possibilities in total.
- Starting position can make the condition hold after all operations are executed. Then each of the three cells on the first robot’s tape has possibilities: for cell , it is either empty in both input and output, or they are equal; for cell , it is either empty in both input and output, or the target output is ; for cell , it is either empty in both input and output, or the output is . So there are possibilities. For the second robot, cell is either empty in both input and output, or the input differs from the target output, giving possibilities; for cells and , they are either empty in both input and output, or the input equals the output, also possibilities each, for a total of possibilities. Therefore, there are possibilities in total.
- Starting position 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 possibility. The second robot has possibilities, so there are possibilities in total.
- Both starting positions satisfy the condition. This requires that for the first robot, cell is empty in both input and output; for cell , input and output are either both empty or both ; for cell , input and output are either both empty or both . So the first robot’s tape has possibilities. For the second robot, cells and are empty, and cell has possibilities; equivalently, cells and must both be empty, and cell is either empty in both input and output, or input equals output, giving possibilities. There are possibilities in total.
- Both starting positions 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 possibility. For the second robot, cells and are empty, and cell has possibilities. There are possibilities in total.
- Both starting positions satisfy the condition. Then the first robot’s tape must be all empty in both input and output, so there is only possibility. For the second robot, cells and are empty, and cell has possibilities. There are possibilities in total.
- Starting positions all satisfy the condition. Then the inputs and outputs of both robots must be all empty. There is possibility in total.
By inclusion-exclusion, the final answer is .
[Constraints]
For all test points: , , .
| Test point ID | Special constraints | ||
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| 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 .
Translated by ChatGPT 5