#P11959. 「ZHQOI R1」诗歌
「ZHQOI R1」诗歌
Description
Given a positive integer , the character set size for this problem is . We use positive integers to denote the -th character in the set.
Given a positive integer , a string is defined as "harmonious" if and only if either:
- The length of is less than ,or
- After deleting any characters from , there exists no contiguous palindromic substring of length greater than in the resulting string.
You need to process queries. For each query, given a "harmonious" string of length and a non-repeating character set , determine the number of strings of length that satisfy the following conditions:
- is a prefix of the string.
- The string is "harmonious".
- The last character of the string belongs to .
Output the answer modulo . For all queries in a test case, and remain the same.
Note: It is guaranteed that .
Input Format
The first line contains an integer representing the test case ID. In sample inputs, .
The second line contains four integers , representing the number of queries, the character set size, and the constant used to define "harmonious" strings.
Each of the next lines describes a query, containing:
- An integer ,
- A sequence of positive integers (the "harmonious" prefix),
- An integer (the size of set ), followed by distinct integers denoting the elements of .
Output Format
For each query, output one integer per line representing the answer modulo .
0
1 5 1
5 1 2 3 1 1
2
0
1 5 1
7 1 2 3 2 1 2
6
0
1 40 4
50 2 3 5 7 11 31 2 5 10
732767443
0
1 12 1
12 3 5 7 1 7
32390928
Hint
Sample 1 Explanation
The prefix is abc (encoded as 1 2 3), , and the character set is . The valid strings of length 5 are:
abcda
abcea
Hence the answer is 2.
Sample 2 Explanation
The prefix is abc, . Among valid strings of length 7, exactly 6 meet the conditions.
abceadb
abcdeab
abcedba
abcdeba
abcedab
abcdaeb
Hence the answer is 6.
Constraints
For all test cases: ,,,,。
| Subtask | Additional Constraints | ||||
|---|---|---|---|---|---|
| 1 | |||||
| 2 | None | ||||
| 3 | |||||
| 4 | |||||
| 5 | |||||
| 6 | |||||
| 7 | |||||
| 8 | |||||
| 9 | |||||
| 10 | |||||
京公网安备 11011102002149号