Codeforces Round 1082 (Div. 1)
C
Rigged Bracket Sequence
Codeforces
2000
原题
← BD →
翻译中…

A regular bracket sequence is a sequence consisting of '(' and ')', which can be turned into a valid math expression by inserting $1$ and $+$ any number of times into the sequence. For example, the sequence "()(()())" is a regular bracket sequence, while "())(()" or "(()" are not regular bracket sequences.

翻译中…

You are given a regular bracket sequence $S$.

翻译中…

Let us consider shifting a subsequence$^{\text{∗}}$ to the right. Formally, when a subsequence $S_{i_1} S_{i_2} \ldots S_{i_k}$ is shifted to the right, the characters on the chosen indices simultaneously get reassigned as follows:

翻译中…
  • $S_{i_1} \leftarrow S_{i_k}$;
  • $S_{i_2} \leftarrow S_{i_1}$;
  • $S_{i_3} \leftarrow S_{i_2}$;
  • $\ldots$
  • $S_{i_k} \leftarrow S_{i_{k-1}}$.
翻译中…

In other words, the element of the $j$-th chosen index gets reassigned to the $((j-2) \bmod k + 1)$-th chosen character.

翻译中…

For example, when $S$ is "()(()())", shifting the subsequence $S_2 S_4$ changes $S$ to "((())())". On the other hand, shifting $S_2 S_3 S_5$ changes $S$ to "())((())".

翻译中…

Please count how many non-empty subsequences make $S$ remain regular when shifted to the right. As the answer may be huge, you are only asked to output the answer modulo $998\,244\,353$.

翻译中…

$^{\text{∗}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) element from arbitrary positions. Two subsequences are considered different if the sets of positions of the deleted elements are different.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 300\,000$, $n$ is even).

翻译中…

The second line of each test case contains a regular bracket sequence $S$ of length $n$ given as a string without spaces.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $300\,000$.

翻译中…

Output

翻译中…

For each test case, output the answer modulo $998\,244\,353$ on a separate line.

翻译中…

Note

翻译中…

For the second test case, the $8$ non-empty subsequences that make $S$ remain regular when shifted to the right are as follows:

翻译中…
  • $S_1$: changes $S$ to "()()";
  • $S_2$: changes $S$ to "()()";
  • $S_3$: changes $S$ to "()()";
  • $S_4$: changes $S$ to "()()";
  • $S_1 S_3$: changes $S$ to "()()";
  • $S_2 S_3$: changes $S$ to "(())";
  • $S_2 S_4$: changes $S$ to "()()";
  • $S_1 S_2 S_3$: changes $S$ to "(())".
翻译中…

样例

Input #1
4
2
()
4
()()
6
(()())
10
()((())())
Output #1
2
8
28
312
InputCopy OutputCopy
题目信息
题号
2201C
时间限制
2 s
内存限制
256 MB
标签
组合数学
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。