杭电暑期多校集训—Killer Names

来源:互联网 发布:java面向对象编程 编辑:程序博客网 时间:2024/06/07 06:54

Killer Names

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


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
题意:
给出长度为m的字符串,组成长度都为n的名和姓,要求名和姓不能有重复的字符,且名和姓一样长,问有多少种组合名字的方法。
分析:
我们可以假设在姓中选 i 个字符,在名中选 j 个字符能构成的不同的名字有多少个,选出来的种数就有 C(m,i) * C(m-i ,j)种。
再考虑 i 个字符在n个位置的排列,每一个位置都有 i 个字符能放,所以就有 (i^n)种,但是此时还要考虑,由于是每个位置都有 i 种选择,那么,假设n=3,i = 3的时候(i^n)就会包括 aaa,bbb,ccc,这种情况,仔细一想,这就是i=1的情况呀,然后还会包括aab,abb,aba,aac,acc,aca(不一一列举了)。然后这就是i=2的情况呀。那么以此类推,我们算到第i个字符的时候,
要减去(i - 1),(i - 2) ……1里面的所有情况。那么要减去多少个呢?再考虑,当前我要放 i 个字符,那么就会有C(i,1)个 1 字符,
有C(i , 2)个包含两个字符的。。。以此类推,所以i个字符在 n个位置的排列个数就可以求出来了,设为 f[ i ] 。
这里只要对不同 n 每次做一下预处理。最后再枚举前面说的 i , j 将答案累加。
(f[ i ] * f[ j ])  * C(m,i) * C(m-i ,j)。再注意爆LL的情况就好。
#include <bits/stdc++.h>#define siz 1005const int 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,0,sizeof(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++)        {///给姓分配i种字符,名分配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;}