#abc363c. C - Avoid K Palindrome 2

C - Avoid K Palindrome 2

Score : 300300 points

问题陈述

给定一个由小写英文字母组成的字符串 SS,长度为 NN

找出通过排列字符串 SS 的字符得到的字符串数量(包括字符串 SS 本身),这些字符串不包含长度为 KK 的回文子串。

这里,如果存在一个非负整数 ii,且 ii 不大于 (NK)(N-K),使得对于每一个整数 jj,满足 1jK1 \leq j \leq K,都有 Ti+j=Ti+K+1jT_{i+j} = T_{i+K+1-j},则称长度为 NN 的字符串 TT "包含长度为 KK 的回文子串"。 在这里,TkT_k 表示字符串 TT 的第 kk 个字符。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a string SS of length NN consisting only of lowercase English letters.

Find the number of strings obtained by permuting the characters of SS (including the string SS itself) that do not contain a palindrome of length KK as a substring.

Here, a string TT of length NN is said to "contain a palindrome of length KK as a substring" if and only if there exists a non-negative integer ii not greater than (NK)(N-K) such that Ti+j=Ti+K+1jT_{i+j} = T_{i+K+1-j} for every integer jj with 1jK1 \leq j \leq K.
Here, TkT_k denotes the kk-th character of the string TT.

Constraints

  • 2KN102 \leq K \leq N \leq 10
  • NN and KK are integers.
  • SS is a string of length NN consisting only of lowercase English letters.

Input

The input is given from Standard Input in the following format:

NN KK

SS

Output

Print the number of strings obtained by permuting SS that do not contain a palindrome of length KK as a substring.

Sample Input 1

3 2
aab

Sample Output 1

1

The strings obtained by permuting aab are aab, aba, and baa. Among these, aab and baa contain the palindrome aa of length 22 as a substring.
Thus, the only string that satisfies the condition is aba, so print 11.

Sample Input 2

5 3
zzyyx

Sample Output 2

16

There are 3030 strings obtained by permuting zzyyx, 1616 of which do not contain a palindrome of length 33. Thus, print 1616.

Sample Input 3

10 5
abcwxyzyxw

Sample Output 3

440640
}