#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 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 , and then asks queries on substrings of . Output each answer modulo .
Input Format
The first line contains an integer .
The second line contains a string of length , consisting only of 0, 1, and ?.
The third line contains an integer , the number of queries.
The next lines each contain two integers , meaning a query on the substring .
Output Format
Output lines, each containing one integer: the answer modulo .
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 replacement schemes: the strings 10001 and 10011.
For 10001, there are substrings whose number of distinct subsequences is odd: 10001, which has distinct subsequences; and and $s'_{3,4}, both equal to 00`, each having distinct subsequences.
For 10011, there are 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, has ?, so there are replacement schemes.
Constraints
This problem uses bundled subtasks.
| Special Property | ||||
|---|---|---|---|---|
contains no ? |
||||
For of the testdata, , , and 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 has substrings. For example, the string 121 has 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 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