Hdu 6143 Killer Names【思维+斯特灵数】

来源:互联网 发布:网络创业与实践课答案 编辑:程序博客网 时间:2024/06/06 08:27

Killer Names

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


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 exactlyn 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

题目大意:


有M种字符,每段名字长度为N,一个名字分成两段,现在要求第一段的名字和第二段的名字没有重复的字符,问有多少种分配方式。


思路:


观察到数据范围不大,我们考虑枚举 。


我们首先O(m)枚举一共使用多少种字符,然后再枚举分给第一段使用的字符种类数i,那么剩下的m-i种字符要给第二段名字。


我们考虑分配的字符必须用上,那么对于第一段的问题转化就变成了:有N个不同的物品,要放置在M个不同的盒子中,盒子不能为空,物品都必须使用。

那么问题就很好解决了:

图片转自下边连接:


S是斯特林数。


安利此类问题的一个总结帖:https://bbs.qzzn.com/thread-14856448-1-1.html


Ac代码:

#include <bits/stdc++.h>typedef long long int LL;using namespace std;const int N = 5e3+6;const int MOD = 1e9+7;const int INF = 1e9+7;const double eps = 1e-6;inline int read() {    int x=0,f=1;    char c=getchar();    for(; c<'0'||'9'<c; c=getchar())if(c=='-')f=-1;    for(; '0'<=c&&c<='9'; c=getchar())x=(x<<3)+(x<<1)+c-'0';    return x*f;}#define abs(x) ((x)>0?(x):-(x))#define pii pair<int,int>#define mp  make_pair#define pb  push_back#define x   first#define y   second/********************************************/int n,m;LL Fac[N],Inv[N];LL qmod(LL a,LL b){    LL res = 1ll;    for(;b;b>>=1,a=a*a%MOD)        if(b&1) res=res*a%MOD;    return res;}void init(){    Fac[0] = 1;    for (int i = 1; i <= N; i++) Fac[i] = (Fac[i-1] * i) % MOD;    Inv[N] = qmod(Fac[N], MOD-2);//Fac[N]^{MOD-2}    for (int i = N - 1; i >= 0; i--) Inv[i] = Inv[i+1] * (i + 1) % MOD;}LL C(int n,int m){    return Fac[n]*Inv[m]%MOD*Inv[n-m]%MOD;}const int maxn = 2005;long long s[maxn][maxn];//存放要求的Stirling数const long long mod=1e9+7;//取模void init2()//预处理{    memset(s,0,sizeof(s));    s[1][1]=1;    for(int i=2;i<=maxn-1;i++)        for(int j=1;j<=i;j++)    {        s[i][j]=s[i-1][j-1]+j*s[i-1][j];        if(s[i][j]>=mod)            s[i][j]%=mod;    }}LL S(int n,int m){    return Fac[m]*s[n][m]%MOD;}int main(){    init();    init2();    int _ = 1;    for(scanf("%d",&_);_--;){        scanf("%d%d",&n,&m);        LL ans = 0;        for(int i=2;i<=m;i++){            for(int j=1;j<i;j++){                ans+=S(n,j)*S(n,i-j)%MOD*C(m,i)%MOD*C(i,j)%MOD;                ans%=MOD;                }        }        printf("%lld\n",ans);    }    return 0;}