CF2201C.Rigged Bracket Sequence

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A regular bracket sequence is a sequence consisting of '(' and ')', which can be turned into a valid math expression by inserting 11 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 SS.

Let us consider shifting a subsequence∗^{\text{∗}} to the right. Formally, when a subsequence Si1Si2…SikS_{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:

  • Si1←SikS_{i_1} \leftarrow S_{i_k};
  • Si2←Si1S_{i_2} \leftarrow S_{i_1};
  • Si3←Si2S_{i_3} \leftarrow S_{i_2};
  • …\ldots
  • Sik←Sik−1S_{i_k} \leftarrow S_{i_{k-1}}.

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

For example, when SS is "()(()())", shifting the subsequence S2S4S_2 S_4 changes SS to "((())())". On the other hand, shifting S2S3S5S_2 S_3 S_5 changes SS to "())((())".

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

∗^{\text{∗}}A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb 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.

合法括号序列是指仅由字符 '(' 和 ')' 构成的序列,且可在其中任意位置插入若干个 1 和 + 后,使其成为一个合法的数学表达式。例如,序列 "()(()())" 是一个合法括号序列,而 ")(()" 或 "((" 则不是合法括号序列。

你被给定一个合法括号序列 SS。

我们考虑将某个子序列∗^{\text{∗}} 向右循环移位。形式化地,当子序列 Si1Si2…SikS_{i_1} S_{i_2} \ldots S_{i_k} 被向右循环移位时,所选下标处的字符将同时按如下方式重新赋值:

  • Si1←SikS_{i_1} \leftarrow S_{i_k};
  • Si2←Si1S_{i_2} \leftarrow S_{i_1};
  • Si3←Si2S_{i_3} \leftarrow S_{i_2};
  • …\ldots
  • Sik←Sik−1S_{i_k} \leftarrow S_{i_{k-1}}。

换言之,第 jj 个被选中的位置上的元素被赋值给第 ((j−2) mod k+1)((j-2) \bmod k + 1) 个被选中的位置上的字符。

例如,当 SS 为 "()(()())" 时,对子序列 S2S4S_2 S_4 进行右移操作后,SS 变为 "((())())";而对子序列 S2S3S5S_2 S_3 S_5 进行右移操作后,SS 变为 "())((())"。

请计算有多少个非空子序列,使得对该子序列进行右移操作后,SS 仍为合法括号序列。由于答案可能非常大,请输出答案对 998 244 353998\,244\,353 取模的结果。

∗^{\text{∗}} 序列 aa 是序列 bb 的一个子序列,当且仅当 aa 可通过从 bb 中删除若干(可能为零个或全部)任意位置的元素得到。若两个子序列所删除的位置集合不同,则认为它们是不同的子序列。

输入格式

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

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

The second line of each test case contains a regular bracket sequence SS of length nn given as a string without spaces.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤300 0002 \le n \le 300\,000,且 nn 为偶数)。

每个测试用例的第二行包含一个长度为 nn 的合法括号序列 SS,以无空格字符串形式给出。

保证所有测试用例的 nn 之和不超过 300 000300\,000。

输出格式

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

对于每个测试用例,在单独一行中输出答案对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    4
    2
    ()
    4
    ()()
    6
    (()())
    10
    ()((())())

    输出#1

    2
    8
    28
    312

说明/提示

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

  • S1S_1: changes SS to "()()";
  • S2S_2: changes SS to "()()";
  • S3S_3: changes SS to "()()";
  • S4S_4: changes SS to "()()";
  • S1S3S_1 S_3: changes SS to "()()";
  • S2S3S_2 S_3: changes SS to "(())";
  • S2S4S_2 S_4: changes SS to "()()";
  • S1S2S3S_1 S_2 S_3: changes SS to "(())".

对于第二个测试用例,使得 SS 在右移后仍保持合法的 88 个非空子序列如下:

  • S1S_1:将 SS 变为 "()()";
  • S2S_2:将 SS 变为 "()()";
  • S3S_3:将 SS 变为 "()()";
  • S4S_4:将 SS 变为 "()()";
  • S1S3S_1 S_3:将 SS 变为 "()()";
  • S2S3S_2 S_3:将 SS 变为 "(())";
  • S2S4S_2 S_4:将 SS 变为 "()()";
  • S1S2S3S_1 S_2 S_3:将 SS 变为 "(())"。

输入解题思路,AI测评打分。不知道怎么写?

首页