#5593. [GCJ 2009 #3] Alphabetomials

[GCJ 2009 #3] Alphabetomials

[GCJ 2009 #3] Alphabetomials

题目描述

As we all know, there is a big difference between polynomials of degree $4$ and those of degree $5$. The question of the non-existence of a closed formula for the roots of general degree $5$ polynomials produced the famous Galois theory, which, as far as the author sees, bears no relation to our problem here.

We consider only the multi-variable polynomials of degree up to 44, over 2626 variables, represented by the set of 2626 lowercase English letters. Here is one such polynomial:

aber+aab+c\text{aber} + \text{aab} + \text{c}

Given a string ss, we evaluate the polynomial on it. The evaluation gives p(S)p(S) as follows: Each variable is substituted with the number of appearances of that letter in SS. For example, take the polynomial above, and let S="abracadabra edgar"S = \text{"abracadabra edgar"}. There are six a's, two b's, one c, one e, and three r's. So

$p(S) = 6 \times 2 \times 1 \times 3 + 6 \times 6 \times 2 + 1 = 109.$

Given a dictionary of distinct words that consist of only lower case letters, we call a string SS a dd-phrase if

S="S1 S2 S3 … Sd",S = "S_1 \ S_2 \ S_3 \ \ldots \ S_d",

where SiS_i is any word in the dictionary, for 1⩽i⩽d1 \leqslant i \leqslant d. i.e., SS is in the form of dd dictionary words separated with spaces. Given a number K⩽10K \leqslant 10, your task is, for each 1⩽d⩽K1 \leqslant d \leqslant K, to compute the sum of p(S)p(S) over all the dd-phrases. Since the answers might be big, you are asked to compute the remainder when the answer is divided by 1000910009.

输入格式

The first line contains the number of cases $T$. $T$ test cases follow. The format of each test case is:

A line containing an expression pp for the multi-variable polynomial, as described below in this section, then a space, then follows an integer KK.

A line with an integer nn, the number of words in the dictionary.

Then nn lines, each with a word, consists of only lower case letters. No word will be repeated in the same test case.

We always write a polynomial in the form of a sum of terms; each term is a product of variables. We write ata^{t} simply as tt a's concatenated together. For example, a2ba^{2} b is written as aabaab. Variables in each term are always lexicographically non-decreasing.

输出格式

For each test case, output a single line in the form

Case #X: sum1sum_{1} sum2sum _{2} …\ldots sumksum _{k}

where XX is the case number starting from 1, and sum i_{i} is the sum of p(S)p(S), where SS ranges over all ii-phrases, modulo 10009.

2
ehw+hwww 5
6
where
when
what
whether
who
whose
a+e+i+o+u 3
4
apple
orange
watermelon
banana
Case #1: 15 1032 7522 6864 253
Case #2: 12 96 576

提示

**Limits**
  • 1⩽T⩽1001 \leqslant T \leqslant 100.
  • The string pp consists of one or more terms joined by '+'. It will not start nor end with a '+'. There will be at most 5 terms for each pp. Each term consists at least 1 and at most 4 lower case letters, sorted in non-decreasing order. No two terms in the same polynomial will be the same.
  • Each word is non-empty, consists only of lower case English letters, and will not be longer than 50 characters. No word will be repeated in the same dictionary.

Small dataset

  • Time limit: 30 3 seconds.
  • 1⩽n⩽201 \leqslant {n} \leqslant 20
  • 1⩽K⩽51 \leqslant {K} \leqslant 5

Large dataset

  • Time limit: 60 6 seconds.
  • 1⩽n⩽1001 \leqslant {n} \leqslant 100
  • 1⩽K⩽101 \leqslant {K} \leqslant 10 标签: P13445|动态规划 DP|2009|Google Code Jam

来源

P13445|[GCJ 2009 #3] Alphabetomials