hdu--6143--Killer Names

来源:互联网 发布:秃鹰配件淘宝 全套 编辑:程序博客网 时间:2024/05/17 04:59

Killer Names

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 522    Accepted Submission(s): 261


Problem Description
> Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith Lord Darth Vader. A powerful Force-user who lived during the era of the Galactic Empire, Marek originated from the Wookiee home planet of Kashyyyk as the sole offspring of two Jedi Knights—Mallie and Kento Marek—who deserted the Jedi Order during the Clone Wars. Following the death of his mother, the young Marek's father was killed in battle by Darth Vader. Though only a child, Marek possessed an exceptionally strong connection to the Force that the Dark Lord of the Sith sought to exploit.
>
> When Marek died in 2 BBY, shortly after the formation of the Alliance, Vader endeavored to recreate his disciple by utilizing the cloning technologies of the planet Kamino. The accelerated cloning process—an enhanced version of the Kaminoan method which allowed for a rapid growth rate within its subjects—was initially imperfect and many clones were too unstable to take Marek's place as the Dark Lord's new apprentice. After months of failure, one particular clone impressed Vader enough for him to hope that this version might become the first success. But as with the others, he inherited Marek's power and skills at the cost of receiving his emotions as well, a side effect of memory flashes used in the training process.
>
> — Wookieepedia

Darth Vader is finally able to stably clone the most powerful soilder in the galaxy: the Starkiller. It is the time of the final strike to destroy the Jedi remnants hidden in every corner of the galaxy.

However, as the clone army is growing, giving them names becomes a trouble. A clone of Starkiller will be given a two-word name, a first name and a last name. Both the first name and the last name have exactly n characters, while each character is chosen from an alphabet of size m. It appears that there are m2n possible names to be used.

Though the clone process succeeded, the moods of Starkiller clones seem not quite stable. Once an unsatisfactory name is given, a clone will become unstable and will try to fight against his own master. A name is safe if and only if no character appears in both the first name and the last name.

Since no two clones can share a name, Darth Vader would like to know the maximum number of clones he is able to create.
 

Input
The First line of the input contains an integer T (T10), denoting the number of test cases. 

Each test case contains two integers n and m (1n,m2000).
 

Output
For each test case, output one line containing the maximum number of clones Vader can create.

Output the answer  mod 109+7
 

Sample Input
23 22 3
 

Sample Output
2 18

题目大意:

起名字,名字由名和姓组成两部分,名由n个字母组成,姓也由n个字母组成,但有要求:可以用的字母一共有m种,在名和姓的内部可以有重复的字母,但是名和姓之间不能有重复的字母,问一共有多少种可能的组合。

解题思路:

根据给名和姓分配不同的字母种类数进行分类讨论,比如给名分配i种字母,给姓分配j种字母。则不同的分配方法有c[m][i]*c[m-i][j]种,再具体到名有n个位置,分配i种字母,假设共有f(i)种情况,同理,姓一共有f(j)种,那么总的情况就是c[m][i]*c[m-i][j]*f(i)*f(j);

下面我们的任务就是求f(i)和f(j),拿f(i)来说,就相当于求n个位置,分配i种不同的字母,很容易想到有i^n情况,但这种情况有多算的部分,比如n为3且有a,b,c;三种字母可选,就有可能出现aaa,或者bbb,或者ccc........的情况,这些情况都只用了1个,2个,3个....i-1种。而我们要得是严格使用i种的情况,所以我们就用i^n减去那些只用了1,2,3......(i-1)种字母的情况 只保留使用i种字母的情况,那么只用1种字母的组合数为c[i][1]。2种字母的组合数为c[i][2]......;再分别乘f(1),f(2)....f(i-1),即:c[i][1]*f(1),c[i][2]*f(2),c[i][3]*f(3).....现在假设需要减去的情况和为sum,则上式就可以转化为:c[m][i]*c[m-i][j]*(i^n-sum1)*(j^n-sum2);再枚举i和j的情况,进行累加,就是答案。

详见代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
#define siz 1005
#define maxn 2000
const long long  mod = 1e9 + 7;
typedef long long LL;
using namespace std;
int n, m;
LL P[maxn + 5][maxn + 5], f[maxn + 5], C[maxn + 5][maxn + 5];
void Init()
{
    for(LL i = 1; i <= maxn; i++)
    {
        P[i][0] = 1;
        for(int j = 1; j <= maxn; j++)
            P[i][j] = P[i][j - 1] * i % mod;
    }
    for(int i = 0; i <= maxn; i++)
    {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++)
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    }
}
void solve()
{
    LL sum = 0;
    memset(f, 0sizeof(f));
    for(int i = 1; i <= m && i <= n; i++)
    {
        sum = 0;
        for(int j = 1; j <= i; j++)
        {
            sum = (sum + f[j] * C[i][j] % mod) % mod; ///将要减掉的累加
        }
        f[i] = (P[i][n] - sum + mod) % mod;
    }
    LL ans = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(i + j > m) break;
            ans = (ans + (((f[i] * f[j]) % mod) * ((C[m][i] * C[m - i][j]) % mod)) % mod) % mod;
        }
    }
    printf("%I64d\n", ans);
}
int main()
{
    int T;
    Init();
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &m);
        solve();
    }
    return 0;
}