#abc359d. D - Avoid K Palindrome
D - Avoid K Palindrome
Score : points
问题陈述
你得到了一个由字符 A
、B
和 ?
组成的长度为 的字符串 。
你还得到了一个正整数 。如果一个由 A
和 B
组成的字符串 满足以下条件,则被认为是一个好字符串:
- 没有长度为 的连续子串在 中是回文。
设 为 中 ?
字符的数量。通过将 中的每个 ?
替换为 A
或 B
,可以得到 个字符串。找出这些字符串中有多少个是好字符串。
计数可能会非常大,因此需要找到它对 取模的结果。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a string of length consisting of characters A
, B
, and ?
.
You are also given a positive integer . A string consisting of A
and B
is considered a good string if it satisfies the following condition:
- No contiguous substring of length in is a palindrome.
Let be the number of ?
characters in . There are strings that can be obtained by replacing each ?
in with either A
or B
. Find how many of these strings are good strings.
The count can be very large, so find it modulo .
Constraints
- is a string consisting of
A
,B
, and?
. - The length of is .
- and are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
7 4
AB?A?BA
Sample Output 1
1
The given string has two ?
s. There are four strings obtained by replacing each ?
with A
or B
:
ABAAABA
ABAABBA
ABBAABA
ABBABBA
Among these, the last three contain the contiguous substring ABBA
of length 4, which is a palindrome, and thus are not good strings.
Therefore, you should print 1
.
Sample Input 2
40 7
????????????????????????????????????????
Sample Output 2
116295436
Ensure to find the number of good strings modulo .
Sample Input 3
15 5
ABABA??????????
Sample Output 3
0
It is possible that there is no way to replace the ?
s to obtain a good string.
Sample Input 4
40 8
?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA
Sample Output 4
259240