#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 S1S_1 and S2S_2 are both legal bracket sequences, then S1+S2S_1+S_2 is a legal bracket sequence.
  • If SS is a legal bracket sequence, then (+S+)(+S+) is a legal bracket sequence.
  • If SS is a legal bracket sequence, then inserting a space at any position in SS (including the beginning and the end) yields a legal bracket sequence.

Now Alice wants to know: for certain states ss and tt in a given finite state automaton, how many legal bracket sequences of length kk start at ss and end at tt.

A finite state automaton is a directed graph GG with nn 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 00. For the ii-th state, let dfai,0/1/2dfa_{i,0/1/2} denote the target state of the category representing left parenthesis, right parenthesis, and space, respectively, and let dfa2i,0/1/2dfa2_{i,0/1/2} denote the number of edges in each category. For a path starting from ss and ending at tt, if it has length kk 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 [G,s,t,k][G,s,t,k]."

Now Alice provides Bob with the automaton GG and poses QQ queries. For each query, Alice will give s,t,ks,t,k, and she hopes Bob can tell her how many legal bracket sequences satisfy [G,s,t,k][G,s,t,k]. She only needs the answer modulo 4747.

Input Format

The first line contains an integer nn denoting the number of states. Lines 22 through n+1n+1, where the ii-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 i1i-1.

The next line contains an integer qq denoting the number of queries. Each of the following qq lines contains three integers s,t,ks,t,k describing one query.

Output Format

Output qq lines, each containing one integer, the answer for the corresponding query mod 47\bmod\ 47.

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 33 are:

  • ___, with the number of legal options being 33=273^3 = 27.
  • _(), (_), ()_, each with the number of legal options being 1×2×3=61 \times 2 \times 3 = 6.

Therefore, the total number of options is 27+6×3=4527 + 6 \times 3 = 45.

Constraints:

For 100%100\% of the testdata, 1n21 \leq n \leq 2, 0dfai,j,s,t<n0 \leq dfa_{i,j}, s, t < n, 0dfa2i,j<2310 \leq dfa2_{i,j} < 2^{31}, 1k1051 \leq k \leq 10^5.

Translated by ChatGPT 5