hdu6143

来源:互联网 发布:柠檬网络电视tv免费版 编辑:程序博客网 时间:2024/06/05 20:34

Killer Names

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


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

参考:http://blog.csdn.net/algzjh/article/details/77341526

解题思路:考虑第一组盒子,假设用了i种颜色的球,那么设f(i)为用i种颜色的球(每种颜色必须用到)填n个盒子的种数,显然f(1)=1,且有f(i)=i^n−C1if(1)−C2if(2)−⋯−Ci−1if(i−1),那么第二组盒子就是用(m−i)种颜色的球(不要求每种颜色都用到)填入n个盒子的种数,即(m−i)n,根据乘法原理,总的方法数就是二者的乘积。枚举第一组盒子的颜色种数就行,注意的是,当m≤n时,第一组盒子用的颜色范围为[1,m−1],当m>n时,第一组盒子用的颜色范围为[1,n]。组合数提前打表(要求i种球填n个盒子,先求出左右情况,再选出其中i-1个球的填充,i-2个球的填充……最终得到i个球全部使用的填充n个盒子的种数)。

#include<iostream>#include<string.h>#include<algorithm>using namespace std;#define mods 1000000007int n,m;long long _power(long long a ,long long b)       //快速幂 {    long long res=1;    while(b)    {        if(b & 1)             res =res*a%mods;        a =a*a%mods;        b>>=1;    }    return res;}long long C[2005][2005];long long f[2005];       //用用i种颜色的球(每种颜色必须用到)填n个盒子的种数 void calculateC(){for(int i=1;i<=2005;i++)    {        C[0][i]=C[i][i]=1;        for(int j=1;j<i;j++)        {            C[j][i]=(C[j-1][i-1]+C[j][i-1])%mods;    //用动态规划求出组合数的转移方程         }    }}void solve(){f[1]=1;for(int i=2;i<=n;i++){long long temp=0;for(int j=1;j<i;j++){temp=(temp+(C[j][i]*f[j])%mods)%mods;}f[i]=(_power(i,n)-temp+mods)%mods;}int cnt;long long ans=0;int k=m<=n?m-1:n;for(int i=1;i<=k;i++){cnt=(((C[i][m]*f[i])%mods)*_power(m-i,n))%mods;ans+=cnt;ans%=mods;}cout<<ans<<endl;}int main(){int t;cin>>t;calculateC();while(t--){cin>>n>>m;solve();}return 0;}




原创粉丝点击