#P3310. [SDOI2014] 括号序列计数
[SDOI2014] 括号序列计数
Description
Alice and Bob know that a sequence consisting of spaces, left parentheses, and right parentheses is called a bracket sequence. A special type of bracket sequence is called a "legal bracket sequence." It is known that:
- The empty string is a legal bracket sequence.
- If and are both legal bracket sequences, then is a legal bracket sequence.
- If is a legal bracket sequence, then is a legal bracket sequence.
- If is a legal bracket sequence, then inserting a space at any position in (including the beginning and the end) yields a legal bracket sequence.
Now Alice wants to know: for certain states and in a given finite state automaton, how many legal bracket sequences of length start at and end at .
A finite state automaton is a directed graph with nodes, each node representing a state, and there are three categories of outgoing directed edges from each state. For every state, all outgoing edges of the same category point to the same state. The three categories correspond to three symbols: left parenthesis, right parenthesis, and space.
We number the states starting from . For the -th state, let denote the target state of the category representing left parenthesis, right parenthesis, and space, respectively, and let denote the number of edges in each category. For a path starting from and ending at , if it has length and the sequence of symbols corresponding to the edges on the path forms a legal bracket sequence, we call it "a legal bracket sequence that satisfies ."
Now Alice provides Bob with the automaton and poses queries. For each query, Alice will give , and she hopes Bob can tell her how many legal bracket sequences satisfy . She only needs the answer modulo .
Input Format
The first line contains an integer denoting the number of states. Lines through , where the -th of them contains six integers $dfa_{i-1,0},dfa2_{i-1,0},dfa_{i-1,1},dfa2_{i-1,1},dfa_{i-1,2},dfa2_{i-1,2}$, describe the outgoing edges of state .
The next line contains an integer denoting the number of queries. Each of the following lines contains three integers describing one query.
Output Format
Output lines, each containing one integer, the answer for the corresponding query .
1
0 1 0 2 0 3
6
0 0 3
0 0 4
0 0 5
0 0 6
0 0 7
0 0 8
45
9
10
2
19
25
Hint
In the sample explanation, the symbol _ represents a space.
For the first query, the legal bracket sequences of length are:
___, with the number of legal options being ._(),(_),()_, each with the number of legal options being .
Therefore, the total number of options is .
Constraints:
For of the testdata, , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号