Killer Names

来源:互联网 发布:怎样提高淘宝店排名 编辑:程序博客网 时间:2024/05/01 08:15

1011-Killer Names

*-HDU-2017 Multi-University Training Contest - Team 8*

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 m^{2n} 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 (T\le 10), denoting the number of test cases.

Each test case contains two integers n and m (1\le n, m \le 2000).

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

Output the answer \ mod\ 10^9+7

Sample Input

2
3 2
2 3

Sample Output

2
18

Solution

Permulation+Stiriling
题意:起名。姓、名各有n个位,共有m个字符可以选择。姓与名不可以含有相同字母。问总共有多少起名方式。n,m<=2000。
首先给姓选择字符,分别可选i=1,2,..,.m-1种。考虑到字符应当放到具体位上去,分别乘S[n][i]。
相应地,名可有j=1..m-1,1..m-2,…,1种。各自乘上S[n][j]再求和,将和与S[n][i]相乘。
累加。
注意对1e9+7取模。

Code

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int mx = 2010;const ll mod = 1e9+7;int T,m,n;ll ans,S[mx][mx],s,t,f[mx],tmp;int  c[2051][2051];int main() {    for (int i=0; i<=2002; i++)        for (int j=0; j<=i; j++)            if (j==0||i==j) c[i][j]=1;            else c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;    f[1]=1;    for (int i=2; i<2001; ++i) f[i]=f[i-1]*i%mod;    for (int i=0; i<2001; ++i) S[i][i]=1;    for (int i=0; i<2001; ++i) {        for (int j=1; j<i; ++j) {            if (i==j) continue;            S[i][j]=(S[i-1][j]*j+S[i-1][j-1])%mod;        }    }    scanf("%d",&T);    while (T--) {        scanf("%d%d",&n,&m);        ans=0;        for (int i=1; i<=min(m-1,n); ++i)  {            s=0;            t=((c[m][i]%mod)*S[n][i])%mod;            t=t*f[i]%mod;            for (int j=1; j<=min(m-i,n); ++j) {                tmp=f[j]*c[m-i][j]%mod;                s=(s+tmp*S[n][j])%mod;            }            ans=(ans+(s*t))%mod;        }        printf("%I64d\n",ans);    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 头发油的特别快怎么办 头发很油 又少怎么办 头发两天就油了怎么办 头发被剪的很丑怎么办 美发师年龄大了怎么办 青春期头发掉的厉害怎么办 剪了短发头发翘怎么办 4个月婴儿咳嗽怎么办 2个月婴儿咳嗽怎么办 5个月婴儿咳嗽怎么办 烫的内扣睡觉怎么办 站了一天腿疼怎么办 头发被剪得很丑怎么办 淹脖子破皮了怎么办 孩子胖脖子淹了怎么办 宝宝屁股破皮了怎么办 新生儿屁屁发红及烂怎么办 头发烫完太蓬松怎么办 头发烫的太卷了怎么办 烫了卷发很毛躁怎么办 烫发后头发干枯毛躁怎么办 烫完头发掉发怎么办 烫发掉发严重该怎么办 烫头发后掉头发怎么办 烫过头发后毛躁怎么办 烫了头发像大妈怎么办 头发烫了之后很毛躁怎么办 鳄龟爪子烂了怎么办 乌龟壳边缘烂了怎么办 乌龟拉绿色的水怎么办 小乌龟龟壳变软怎么办 鳄龟不待在水里怎么办 脖子整天黏黏的怎么办 有脊椎侧弯跳舞怎么办 落枕10天还不好怎么办 落枕十几天没好怎么办 肩一边高一边低怎么办 35岁无稳定工作怎么办 机械手表走的快怎么办 机械表发条紧了怎么办 两只乌龟互相咬怎么办?