Killer Names
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 769 Accepted Submission(s): 377
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.
>
> 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 ).
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
Output the answer mod 109+7
Sample Input
2 3 2 2 3
Sample Output
2 18
题意:一个名字有长度为n的姓和长度为n的名,要从m个字母中选择任意种字符来组成姓名,姓和名中不能有相同的的字符,求最多能有几个名字
思路:首先来考虑一个问题,有i种颜色来染n个格子,且i种颜色必须都用上那有几种方法呢?
第一个格子要的涂法是i种第二个格子也是i种第三个也是i种,那就是i^n种,但是这样会有n个格子都是颜色a,颜色b,或仅有颜色a,b等这些不符合的情况,所以方法数要减去这些也就是i^n-C(i,i-1)(i-1)^n-C(i,i-2)(i-2)^n-......-C(i,1)1^n
第一个格子要的涂法是i种第二个格子也是i种第三个也是i种,那就是i^n种,但是这样会有n个格子都是颜色a,颜色b,或仅有颜色a,b等这些不符合的情况,所以方法数要减去这些也就是i^n-C(i,i-1)(i-1)^n-C(i,i-2)(i-2)^n-......-C(i,1)1^n
设dp[j][i]为在j个格子里面放i种颜色那么有dp[j][i]=i^j-C(i,i-1)(i-1)^j-C(i,i-2)(i-2)^j-......-C(i,1)1^j
然后我们得到了状态方程dp[j][i]=(dp[j-1][i]+dp[j-1][i-1])*i,我们先预处理出dp,然后对于这道题来说我们可以这样考虑
先在从m里面取i种字符,来姓的填n的格子,然后再从m-i中取j中字符来填名的n个格子,那么这样的方法数就是
C[m][i]*dp[n][i]*C[m-i][j]*dp[j],跑一遍循环完事
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const long long mod=1e9+7;
long long C[2005][2005];
long long dp[2005][2005];
void get_C()
{
C[0][0] = 1;
for(int i=1;i<=2000;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 get_dp()
{
for(long long i=1;i<=2000;i++)
dp[i][1]=1;
for(long long i=2;i<=2000;i++)
{
for(long long j=i;j<=2000;j++)
{
dp[j][i]=((dp[j-1][i]*i)%mod+(dp[j-1][i-1]*i)%mod)%mod;
}
}
}
int main()
{
int t;
get_C();
get_dp();
scanf("%d",&t);
while(t--)
{
long long n,m;
scanf("%lld%lld",&n,&m);
long long ans=0;
for(long long i=1;i<m;i++)
{
if(i>n)break;
for(long long j=i;j+i<=m;j++)
{
if(j>n)break;
long long p=C[m][i]*dp[n][i]%mod;
long long q=C[m-i][j]*dp[n][j]%mod;
long long sum=(p*q)%mod;
if(j!=i)
ans=(ans+(long long)2*sum)%mod;
else
ans=(ans+sum)%mod;
}
}
printf("%lld\n",ans);
}
return 0;
}