CF1762C.Binary Strings are Fun

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A binary string†^\dagger bb of odd length mm is good if bib_i is the median‡^\ddagger of b[1,i]§b[1,i]^\S for all odd indices ii (1≤i≤m1 \leq i \leq m).

For a binary string aa of length kk, a binary string bb of length 2k−12k-1 is an extension of aa if b2i−1=aib_{2i-1}=a_i for all ii such that 1≤i≤k1 \leq i \leq k. For example, 1001011 and 1101001 are extensions of the string 1001. String $x=$1011011 is not an extension of string $y=$1001 because x3≠y2x_3 \neq y_2. Note that there are 2k−12^{k-1} different extensions of aa.

You are given a binary string ss of length nn. Find the sum of the number of good extensions over all prefixes of ss. In other words, find ∑i=1nf(s[1,i])\sum_{i=1}^{n} f(s[1,i]), where f(x)f(x) gives number of good extensions of string xx. Since the answer can be quite large, you only need to find it modulo 998 244 353998\,244\,353.

†^\dagger A binary string is a string whose elements are either 0\mathtt{0} or 1\mathtt{1}.

‡^\ddagger For a binary string aa of length 2m−12m-1, the median of aa is the (unique) element that occurs at least mm times in aa.

§^\S a[l,r]a[l,r] denotes the string of length r−l+1r-l+1 which is formed by the concatenation of al,al+1,…,ara_l,a_{l+1},\ldots,a_r in that order.

长度为奇数 mm 的二进制字符串†^\dagger bb 称为好字符串,当且仅当对所有奇数下标 ii(1≤i≤m1 \leq i \leq m),bib_i 是子串 b[1,i]§b[1,i]^\S 的中位数‡^\ddagger。

对于长度为 kk 的二进制字符串 aa,长度为 2k−12k-1 的二进制字符串 bb 称为 aa 的一个扩展,若对所有满足 1≤i≤k1 \leq i \leq k 的 ii,均有 b2i−1=aib_{2i-1} = a_i。例如,1001011 和 1101001 都是字符串 1001 的扩展;而字符串 $x = $1011011 不是字符串 $y = $1001 的扩展,因为 x3≠y2x_3 \neq y_2。注意:aa 共有 2k−12^{k-1} 个不同的扩展。

现给定一个长度为 nn 的二进制字符串 ss。请计算 ss 的所有前缀对应的“好扩展”的数量之和。换言之,求 ∑i=1nf(s[1,i])\sum_{i=1}^{n} f(s[1,i]),其中 f(x)f(x) 表示字符串 xx 的好扩展的个数。由于答案可能非常大,你只需输出其对 998 244 353998\,244\,353 取模的结果。

†^\dagger 二进制字符串是指其每个字符均为 0\mathtt{0} 或 1\mathtt{1} 的字符串。

‡^\ddagger 对于长度为 2m−12m-1 的二进制字符串 aa,其中位数是指在 aa 中出现次数至少为 mm 的(唯一)字符。

§^\S a[l,r]a[l,r] 表示由 al,al+1,…,ara_l, a_{l+1}, \ldots, a_r 按顺序拼接而成的、长度为 r−l+1r-l+1 的字符串。

输入格式

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 (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5), where nn is the length of the binary string ss.

The second line of each test case contains the binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示二进制字符串 ss 的长度。

每个测试用例的第二行包含长度为 nn 的二进制字符串 ss。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print the answer modulo 998 244 353998\,244\,353.

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

输入输出样例

  • 输入#1

    6
    1
    1
    1
    0
    2
    11
    3
    010
    9
    101101111
    37
    1011011111011010000011011111111011111

    输出#1

    1
    1
    3
    3
    21
    365

说明/提示

In the first and second test cases, f(s[1,1])=1f(s[1,1])=1.

In the third test case, the answer is f(s[1,1])+f(s[1,2])=1+2=3f(s[1,1])+f(s[1,2])=1+2=3.

In the fourth test case, the answer is f(s[1,1])+f(s[1,2])+f(s[1,3])=1+1+1=3f(s[1,1])+f(s[1,2])+f(s[1,3])=1+1+1=3.

f(11)=2f(\mathtt{11})=2 because two good extensions are possible: 101 and 111.

f(01)=1f(\mathtt{01})=1 because only one good extension is possible: 011.

在第一个和第二个测试用例中,f(s[1,1])=1f(s[1,1])=1。

在第三个测试用例中,答案为 f(s[1,1])+f(s[1,2])=1+2=3f(s[1,1])+f(s[1,2])=1+2=3。

在第四个测试用例中,答案为 f(s[1,1])+f(s[1,2])+f(s[1,3])=1+1+1=3f(s[1,1])+f(s[1,2])+f(s[1,3])=1+1+1=3。

f(11)=2f(\mathtt{11})=2,因为存在两种可行的“好扩展”:101 和 111。

f(01)=1f(\mathtt{01})=1,因为仅存在一种可行的“好扩展”:011。

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

首页