#P17460. [GESP202609 七级] 括号序列
[GESP202609 七级] 括号序列
Problem Description
For strings and , if deleting any number of characters from can produce , then is a subsequence of . In other words, is formed by selecting some characters from and concatenating them in index order. Two subsequences are different if and only if the sets of chosen indices are different.
For example, sun is a subsequence of sequence, because deleting eq, e, and ce from sequence can produce sun. The string sequence has different subsequences, including the empty string. It also has three different subsequences e, because the nd, th, and th characters of sequence are all e, and keeping each of these characters produces a different subsequence.
For a string , if satisfies the following conditions, then is a valid bracket sequence:
- is the empty string, or
- can be obtained by concatenating
(, a valid bracket sequence, and), or - can be obtained by concatenating two valid bracket sequences.
For example, (), ()(), (()), and (()()) are all valid bracket sequences. But (() and )( are not valid bracket sequences.
Given a string of length that contains only ( and ), find how many of the subsequences of are valid bracket sequences. Since the answer may be very large, output the result modulo .
For example, when is ))(()(, there are subsequences that are valid bracket sequences: the empty string and two different subsequences ().
Input Format
The first line contains a positive integer , indicating the length of the string .
The second line contains a string of length consisting only of ( and ).
Output Format
Output one line containing an integer, representing the number of valid bracket subsequences of modulo .
6
))(()(
3
34
((((((((((((((((()))))))))))))))))
333606220
Hint
For of the testdata, it is guaranteed that .
For all testdata, it is guaranteed that .
Translated by ChatGPT 5