#P10600. BZOJ4350 括号序列再战猪猪侠
BZOJ4350 括号序列再战猪猪侠
Background
This problem comes from the original BZOJ. We acknowledge that the copyright of the statement and the original testdata belongs to the original BZOJ or the problem setter who authorized BZOJ to use it. If you are the copyright owner and believe that we have infringed your rights, please contact us.
Problem Description
A bracket sequence is a sequence consisting only of () . The following bracket sequences are valid:
()is a valid sequence.- If
Ais a valid sequence, then(A)is also a valid sequence. - If both
AandBare valid sequences, thenABis also a valid sequence.
Define as follows: counting from left to right, for the -th left parenthesis, is the index (among right parentheses, from left to right) of the right parenthesis that matches it.
Now you are given a bracket sequence of length . You are also given pieces of information. The -th piece is of the form , meaning .
Ask: based on this information, how many ways are there to restore a valid bracket sequence? Output the answer modulo .
Input Format
The first line contains a positive integer , the number of test cases.
For each test case, the first line contains two positive integers . Here is the number of left parentheses, and is the number of constraints.
The next lines each contain two positive integers .
Output Format
For each test case, output one integer representing the answer.
5
1 0
5 0
3 2
1 2
2 3
3 2
2 1
2 3
3 3
1 2
2 3
3 1
1
42
1
2
0
Hint
For all test cases, it is guaranteed that , , and .
Translated by ChatGPT 5