hdu 6143 Killer Names

来源:互联网 发布:推荐几家淘宝高仿彪马 编辑:程序博客网 时间:2024/05/22 03:50

Killer Names

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


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个字符),前后部分不能有重复的字符,问能组成多少种不同的名字


解析:
先取名字前面的一部分,f[i]表示用i种字符填n位(且每一个字符必须用到)的方案数,asdf[i]=i^n-C(i,1)f[1]-C(i,2)f[2]-....-C(i,i-1)f[i-1]
再取后面一部分,剩下的m-i种字符随意组合的方案数为(m-i)^n
将两部分相乘再乘以第一种在m个字符里面随意取i个字符的方案数C(m,i)
这里需要注意第一种枚举的范围,因为前后两部分都需要有字符,所以后面至少有一种字符,所以第一种枚举的范围就是min(n,m-1)

组合数要提前打表的!!!!!!!!!!!!!!!
#include<cstdio>#include<cstring>#define MOD 1000000007using namespace std;typedef long long int ll;const int N = 2010;ll C[N][N],F[N];ll quickpow(ll a,ll b){ll ans=1;while(b){if(b&1) ans=(ans%MOD*(a%MOD))%MOD;a=(a%MOD*(a%MOD))%MOD;b=b>>1;}return ans;}int main(){C[1][1]=1;C[2][1]=2;C[2][2]=1;for(int i=3;i<N;i++)  //组合数打表{C[i][1]=i;for(int j=2;j<i;j++){C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;}C[i][i]=1;}int t,n,m;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);F[1]=1;int Min;if(n>=m-1) Min=m-1;else Min=n;for(int i=2;i<=Min;i++)  //求F[1...min(n,m-1)]{F[i]=quickpow(i,n);for(int j=1;j<i;j++){ll tt=(C[i][j]*F[j])%MOD;if(F[i]<tt) F[i]+=MOD;F[i]=(F[i]-tt)%MOD;}}ll ans=0;for(int i=1;i<=Min;i++){ans=(ans+(C[m][i]*F[i]%MOD)%MOD*quickpow(m-i,n))%MOD;}printf("%lld\n",ans);}return 0;}



原创粉丝点击