2017 杭电多校赛第八场 1011题 Killer Names HDu 6143

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 (T≤10), denoting the number of test cases. 
Each test case contains two integers n and m (1≤n,m≤2000). 
 
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

18

题意:给你 m 个字符用来机器人取 姓 和 名,机器人的 姓 和 名 都由n个字符组成,要求 性 和 名 中没有相同的字符,输出最多有多少种不同点取法。(m,n<2000)


思路: 先规定 姓 中一共有 i 种不同的字符,则这 i 个字符从总共的 m 个字符中取出有 C(m,i) 种不同的取法, 取出后再将这 i 个字符填入长度为 n 的姓中,一共有 a[n][i]【求法在下面】 中不同的填法 , 最后只有 名 的部分,只要将剩下的字符随意 填入即可,共 (m-i)^n 种。

所以最后共有 c(m,i)*a[n]][m]*(m-i)^n 种情况,遍历所有的 i 想加即可得出答案。


a[n][i]求法:打表 ,i=1 时为1, i==n时 为 n!, 否则为 ( a[n-1][i] + a[n-1][i-1] ) * j;


 123456
11     
212    
3166   
41143624  
5130150240120 
816254015601800720

代码:

#include<iostream>
using namespace std;

#define mmx 1000000007;

long long a[2005][2005], yh[2005][2005];

int main ()
{
    long long i,j,T;
//	杨辉三角打表 
    for(i=0;i<2005;i++)
    {
    	yh[i][1]=1;
    	yh[i][i]=1;
	}
    for(i=2;i<2005;i++)
        for(j=2;j<i;j++)
            yh[i][j]=(yh[i-1][j-1]+yh[i-1][j])%mmx;  
            
//	长度为 m 的姓用了 i 个字母时一共有 a[m][i] 种情况 	
    for(i=1;i<2002;i++)
        for(j=1;j<=i;j++)
        {
            if(j==1)
                a[i][j]=1;
            else if(j==i)
            {
            	a[i][j]=1;
                for(int k=1;k<=j;k++)
                {
                        a[i][j]*=k;
                        a[i][j]%=mmx;
                }
            }
            else
                a[i][j]=(a[i-1][j]+a[i-1][j-1])*j;
            a[i][j]=a[i][j]%mmx;
        }
    cin>>T;
    while(T--)
    {
// sn 用来存放姓占用了 i 个字母是时一共有多少种情况 
// ssn 用来存放 一共有多少种情况 
        long long n,m,sn=1,ssn=0; cin>>n>>m;
        for(i=1;i<m;i++) // 表示姓中占用了 i 个字母 
        {  
            sn=yh[m+1][i+1];
            sn=sn*a[n][i]%mmx;
            for(j=0;j<n;j++) 
                sn=sn*(m-i)%mmx;
            ssn+=sn%mmx; 
            ssn=ssn%mmx;
            sn=1;
        }
        cout<<ssn<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值