#P8885. 「JEOI-R1」子序列

「JEOI-R1」子序列

Background

Problem Setter Reference Solution testdata Validator Editorial
Pekemetier cq_zry & lOlAKME Pekemetier

Problem Description

You are given a string that contains only 0, 1, and ?. You need to replace each ? with either 0 or 1. That is, turn it into a 0101 string. Count how many replacement schemes make the resulting string satisfy the following condition:

Among all non-empty substrings, the number of substrings that have an odd number of distinct subsequences (including the empty string) is odd.

In particular, if the string does not contain ?, then the string itself should be treated as the only replacement scheme.

Each test gives a string ss, and then asks queries on substrings of ss. Output each answer modulo 998244353998244353.

Input Format

The first line contains an integer nn.

The second line contains a string ss of length nn, consisting only of 0, 1, and ?.

The third line contains an integer mm, the number of queries.

The next mm lines each contain two integers l,rl, r, meaning a query on the substring sl,rs_{l,r}.

Output Format

Output mm lines, each containing one integer: the answer modulo 998244353998244353.

If there is no valid scheme, output 0.

5
100?1
5
1 5
1 4
2 5
3 4
1 3
1
0
1
1
1
20
1110??01001010?1?110
20
1 20
5 16
11 16
10 13
5 14
13 17
1 18
1 7
6 9
15 19
12 17
17 18
4 11
3 13
13 15
18 19
2 8
7 13
4 15
9 18
3
2
2
0
4
2
13
3
0
1
3
1
2
2
2
1
2
1
1
3

Hint

Sample Explanation

For the first query 1 5 in Sample #1, there are 22 replacement schemes: the strings 10001 and 10011.

For 10001, there are 33 substrings whose number of distinct subsequences is odd: 10001, which has 1515 distinct subsequences; and s2,3s'_{2,3} and $s'_{3,4}, both equal to 00`, each having 33 distinct subsequences.

For 10011, there are 44 substrings whose number of distinct subsequences is odd: 1001, 00, 0011, and 11.

10001 has an odd number of such substrings, so it is counted in the answer; 10011 has an even number, so it is not counted.

For the first query 1 20 in Sample #2, s1,20s_{1,20} has 44 ?, so there are 24=162^4 = 16 replacement schemes.


Constraints

This problem uses bundled subtasks.

Subtask\text{Subtask} nn\leq mm\leq Special Property Score\text{Score}
11 1010 1010
22 100100 ss contains no ?
33 500500 2020
44 10001000
55 50005000 50005000 1010
66 10510^5
77 5×1045\times10^4 3×1053\times 10^5 2020

For 100%100\% of the testdata, 1n5×1041\leq n\leq 5\times10^4, 1m3×1051\leq m\leq 3\times10^5, and ss contains only 0, 1, and ?.


Notes and Explanation

Substring: a string formed by choosing a continuous segment from the original string. A string of length nn has n(n+1)2\frac{n(n+1)}{2} substrings. For example, the string 121 has 66 substrings: 1, 2, 1, 12, 21, 121. Note that in this problem, the empty string is not a substring.

Subsequence: a string formed by deleting any number of characters from the original string (possibly deleting none). For example, the string 0110 has 1111 distinct subsequences: the empty string, 0, 1, 00, 01, 10, 11, 010, 011, 110, 0110. Note that in this problem, the empty string is also considered a subsequence.

Translated by ChatGPT 5