HDU 6143 Killer Names

Killer Names

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


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
2 3 2 2 3
 
Sample Output
2 18
 
Source
 
/*
* @Author: lyuc
* @Date:   2017-08-17 11:02:49
* @Last Modified by:   lyuc
* @Last Modified time: 2017-08-17 15:07:25
*/
/*
 题意:一个人的名字由名和姓组成,每个部分都严格的有n个字母,现在有m个字母可以选择,
    并且名和姓不能有相同的字母

 思路:枚举姓中用的字母数量,最小1,最大min(n,m)个,现在的问题就是 i (i<=n)个字母可
    重复的使用能组成多少种名字,这里用到了容斥(看了两天的容斥,竟然用到了,哈哈哈)
    如果不考虑i个必须都用到,那么就有i^n中情况,但是中间多了很多这种,要减去最多用
    i-1种字母的情况,但是又减多了,最后总的情况就是:
        res=C(i,i)*i^n-C(i,i-1)*(i-1)^n+C(i,i-2)*(i-2)^n...C(i,1)*1^n;
    然后这是姓的情况,剩下m-i个字母在名中可以随意用所以就是,(m-i)^n
*/

#include <bits/stdc++.h>

#define LL long long
#define MAXN 2010
const LL MOD=1e9+7;

using namespace std;

int t;
LL n,m;
LL len;
LL res;

LL F[MAXN], Finv[MAXN], inv[MAXN];//F是阶乘,Finv是逆元的阶乘
LL X[MAXN][MAXN];

void init(){
    memset(F,0,sizeof F);
    memset(Finv,0,sizeof Finv);
    memset(inv,0,sizeof inv);
    memset(X,0,sizeof X);
    inv[1] = 1;
    for(LL i = 2; i < MAXN; i ++){
        inv[i] = (MOD - MOD / i) * 1LL * inv[MOD % i] % MOD;
    }
    F[0] = Finv[0] = 1;
    for(LL i = 1; i < MAXN; i ++){
        F[i] = F[i-1] * 1LL * i % MOD;
        Finv[i] = Finv[i-1] * 1LL * inv[i] % MOD;
    }
    X[0][0]=0;
    for(LL i=1;i<=2000;i++){
        X[i][0]=1;
        for(LL j=1;j<=2000;j++){
            X[i][j]=(X[i][j-1]%MOD*i)%MOD;
        }
    }
}

LL Comb(LL n,LL m){//comb(n, m)就是C(n, m) 
    if(m < 0 || m > n) return 0;
    return F[n] * 1LL * Finv[n - m] % MOD * Finv[m] % MOD;
}
//138625375
int main(){
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&n,&m);
        res=0;
        len=min(n,m);
        
        for(LL i=1;i<=len;i++){//枚举名字中放的字母数
            LL cnt=0;
            LL F=1;
            for(LL j=i;j>=1;j--){//容斥求i个物品,放在n个格子内的种类数
                cnt=((cnt+Comb(i,j)*F*X[j][n]+MOD)%MOD)%MOD;
                F *=-1;
            }
            //             [这个是取出i个字母放到名中] [这是剩余的字母在后n中随便放]         
            res = ( (res + cnt * Comb(m,i) % MOD * X[m - i][n] +MOD) % MOD) % MOD;
        }
        printf("%lld
",res);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7384340.html