#P16845. [GKS 2021 #C] Smaller Strings

    ID: 17089 Type: RemoteJudge 2000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>字符串数学2021Google Kick Start

[GKS 2021 #C] Smaller Strings

题目描述

You are given an integer KK and a string SS of length NN, consisting of lowercase letters from the first KK letters of the English alphabet. Find the number of palindrome strings of length NN which are lexicographically smaller than SS and consist of lowercase letters from the first KK letters of the English alphabet.

A string composed of ordered letters a1,a2,…,ana_1, a_2, \ldots, a_n is lexicographically smaller than another string of the same length b1,b2,…,bnb_1, b_2, \ldots, b_n if ai<bia_i < b_i, where ii is the first index where characters differ in the two strings. For example, the following strings are arranged in lexicographically increasing order: aaa, aab, aba, cab.

A palindrome is a string that is the same written forwards and backwards. For example, anna, racecar, aaa and x are all palindromes, while ab, frog and yoyo are not.

As the number of such strings can be very large, print the answer modulo 109+710^9 + 7.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case consists of two lines. The first line contains two integers NN and KK. The second line contains a string SS of length NN, consisting of lowercase letters.

输出格式

For each test case, output one line containing Case #xx: followed by yy, where xx is the test case number (starting from 11) and yy is the number of lexicographically smaller palindrome strings modulo 109+710^9 + 7.

3
2 3
bc
5 5
abcdd
1 5
d
Case #1: 2
Case #2: 8
Case #3: 3

提示

In Sample Case #1, the palindromes are ["aa", "bb"].

In Sample Case #2, the palindromes are ["aaaaa", "aabaa", "aacaa", "aadaa", "aaeaa", "ababa", "abbba", "abcba"].

In Sample Case #3, the palindromes are ["a", "b", "c"].

Limits

1≤T≤1001 \le T \le 100.

The string SS consists of lowercase letters from the first KK letters of the English alphabet.

Test Set 11

1≤N≤81 \le N \le 8.

1≤K≤51 \le K \le 5.

Test Set 22

1≤N≤1051 \le N \le 10^5.

1≤K≤261 \le K \le 26.