CF1762C.Binary Strings are Fun
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A binary string† b of odd length m is good if bi is the median‡ of b[1,i]§ for all odd indices i (1≤i≤m).
For a binary string a of length k, a binary string b of length 2k−1 is an extension of a if b2i−1=ai for all i such that 1≤i≤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=y2. Note that there are 2k−1 different extensions of a.
You are given a binary string s of length n. Find the sum of the number of good extensions over all prefixes of s. In other words, find ∑i=1nf(s[1,i]), where f(x) gives number of good extensions of string x. Since the answer can be quite large, you only need to find it modulo 998244353.
† A binary string is a string whose elements are either 0 or 1.
‡ For a binary string a of length 2m−1, the median of a is the (unique) element that occurs at least m times in a.
§ a[l,r] denotes the string of length r−l+1 which is formed by the concatenation of al,al+1,…,ar in that order.
长度为奇数 m 的二进制字符串† b 称为好字符串,当且仅当对所有奇数下标 i(1≤i≤m),bi 是子串 b[1,i]§ 的中位数‡。
对于长度为 k 的二进制字符串 a,长度为 2k−1 的二进制字符串 b 称为 a 的一个扩展,若对所有满足 1≤i≤k 的 i,均有 b2i−1=ai。例如,1001011 和 1101001 都是字符串 1001 的扩展;而字符串 $x = $1011011 不是字符串 $y = $1001 的扩展,因为 x3=y2。注意:a 共有 2k−1 个不同的扩展。
现给定一个长度为 n 的二进制字符串 s。请计算 s 的所有前缀对应的“好扩展”的数量之和。换言之,求 ∑i=1nf(s[1,i]),其中 f(x) 表示字符串 x 的好扩展的个数。由于答案可能非常大,你只需输出其对 998244353 取模的结果。
† 二进制字符串是指其每个字符均为 0 或 1 的字符串。
‡ 对于长度为 2m−1 的二进制字符串 a,其中位数是指在 a 中出现次数至少为 m 的(唯一)字符。
§ a[l,r] 表示由 al,al+1,…,ar 按顺序拼接而成的、长度为 r−l+1 的字符串。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105), where n is the length of the binary string s.
The second line of each test case contains the binary string s of length n.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105),表示二进制字符串 s 的长度。
每个测试用例的第二行包含长度为 n 的二进制字符串 s。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print the answer modulo 998244353.
对于每个测试用例,输出答案对 998244353 取模的结果。
输入输出样例
输入#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])=1.
In the third test case, the answer is f(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=3.
f(11)=2 because two good extensions are possible: 101 and 111.
f(01)=1 because only one good extension is possible: 011.
在第一个和第二个测试用例中,f(s[1,1])=1。
在第三个测试用例中,答案为 f(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=3。
f(11)=2,因为存在两种可行的“好扩展”:101 和 111。
f(01)=1,因为仅存在一种可行的“好扩展”:011。
输入解题思路,AI测评打分。不知道怎么写?