Killer Names
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
Source
题意:m个字符,组成长度为n的两端字符串,要求两端字符串之间没有相同的字符
题解:枚举要为两段字符串分配的字符数目分别为i、j,则在第一段总共有
(mi) 种选取方案,在第二段总共有 (m−ij) 种选取方案。
而在内部,我们设F(n,x)为长度为n的字符串使用x种字符组成的方案数。
则根据容斥原理 F(n,x)=xn−(x1)(x−1)n+(x2)(x−2)n−(x3)(x−3)n+...
于是最终的结果便是所有 (mi)F(n,i)×(m−ij)F(n,j) 之和。
注意:求幂时要记忆化,否则会t
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack>
#include<algorithm>
#define ll long long
#define INF 1e9
using namespace std;
ll c[2005][2005];
ll mod=1e9+7;
ll p[2005][2005];
ll quickpow(ll n,ll m)
{
if(p[n][m]) return p[n][m];
ll nn=n;
ll mm=m;
ll ans=1;
n%=mod;
while(m)
{
if(m%2==1)
{
ans=ans*n;
ans=ans%mod;
m=m-1;
}
else
{
n=(n*n)%mod;
n%=mod;
m=m/2;
}
}
p[nn][mm]=ans;
return ans;
}
void initcom()
{
memset(c, 0, sizeof(c));
for (int i = 0; i <2005; i++)
c[i][0] = 1;
c[1][1] = 1;
for (int i = 2; i <2005; i++) {
for (int j = 1; j <2005; j++) {
if (j == i) {
c[i][j] = 1;
break;
}
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
}
}
}
ll get(ll n,ll x)
{
ll ans=0;
ll f=1;
for(ll i=0;i<=x;i++)
{
ans+=(c[x][i]*quickpow(x-i,n)*f)%mod;
ans=(ans+mod)%mod;
f*=-1;
}
return ans;
}
ll r[2005];
int main()
{
int t;
initcom();
scanf("%d",&t);
memset(p,0,sizeof(p));
while(t--)
{
ll n,m;
scanf("%lld%lld",&n,&m);
memset(r,0,sizeof(r));
for(ll i=1;i<m;i++)
r[i]=get(n,i);
ll ans=0;
for(ll i=1;i<m;i++)
for(ll j=i;j<=m-i;j++)
{
ll a=c[m][i]*r[i]%mod;
ll b=c[m-i][j]*r[j]%mod;
a=(a*b)%mod;
if(j!=i) a=(a+a)%mod;
ans+=a;
ans%=mod;
}
cout<<ans<<endl;
}
return 0;
}