#P16880. [GKS 2022 #C] Palindromic Deletions

    ID: 19208 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2022区间 DP期望Google Kick Start

[GKS 2022 #C] Palindromic Deletions

Problem Description

Games with words and strings are very popular lately. Now Edsger tries to create a similar new game of his own. Here is what he came up with so far.

Edsger's new game is called Palindromic Deletions. As a player of this game, you are given a string of length NN. Then you will perform the following process NN times:

  1. Pick an index in the current string uniformly at random.
  2. Delete the character at that index. You will then end up with a new string with one fewer character.
  3. If the new string is a palindrome, you eat a piece of candy in celebration.

Now Edsger wonders: given a starting string, what is the expected number of candies you will eat during the game?

Input Format

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 of each test case contains an integer NN, representing the length of the string.

The second line of each test case contains a string SS of length NN, consisting of lowercase English characters.

Output Format

For each test case, output one line containing Case #xx: EE, where xx is the test case number (starting from 11) and EE is the expected number of candies you will eat during the game.

EE should be computed modulo the prime 109+710^9 + 7 (10000000071000000007) as follows. Represent the answer of a test case as an irreducible fraction pq\frac{p}{q}. The number EE then must satisfy the modular equation E×qp(mod(109+7))E \times q \equiv p \pmod{(10^9 + 7)}, and be between 00 and 109+610^9 + 6, inclusive. It can be shown that under the constraints of this problem, such a number EE always exists and can be uniquely determined.

2
2
ab
3
aba
Case #1: 2
Case #2: 333333338

Hint

In the first test case the game can go in one of 22 ways (the character removed at each step is underlined):

  1. "aba\underline{b}" \to "aa" \to "" (where "" denotes empty string). Both aa and "" are palindromes, so you will eat 22 candies.
  2. "ab\underline{a}b" \to "bb" \to "". Both bb and "" are palindromes, so you will eat 22 candies.

Overall, the expected number of candies you will eat is 2+22=2\frac{2+2}{2} = 2 candies.

In the second test case, the game can go in one of 66 ways (the character removed at each step is underlined):

  1. "aba\underline{a}ba" \to "ba\underline{b}a" \to "a\underline{a}" \to ""
  2. "aba\underline{a}ba" \to "bab\underline{a}" \to "b\underline{b}" \to ""
  3. "abaa\underline{b}a" \to "aa\underline{a}a" \to "a\underline{a}" \to ""
  4. "abaa\underline{b}a" \to "aaa\underline{a}" \to "a\underline{a}" \to ""
  5. "abaab\underline{a}" \to "ab\underline{a}b" \to "b\underline{b}" \to ""
  6. "abaab\underline{a}" \to "aba\underline{b}" \to "a\underline{a}" \to ""

Overall, the expected number of candies you will eat is 2+2+3+3+2+26=146=73\frac{2+2+3+3+2+2}{6} = \frac{14}{6} = \frac{7}{3} candies.

333333338333333338 is a uniquely determined number that satisfies the conditions mentioned in the output section as 333333338×37(mod(109+7))333333338 \times 3 \equiv 7 \pmod{(10^9 + 7)}, therefore 333333338333333338 is the answer to this test.

Limits

1T201 \le T \le 20.

String SS consists of only lowercase letters of the English alphabet.

Test Set 11

2N82 \le N \le 8.

Test Set 22

2N4002 \le N \le 400.