HDU6143-Killer Names

Killer Names

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

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
2
18

Source
2017 Multi-University Training Contest - Team 8

题目大意: m 种不同颜色的球,填两组盒子,每组盒子有n个,两组盒子中不能有相同颜色的球,问方法总数有多少。
解题思路:考虑第一组盒子,假设用了 i 种颜色的球,那么设f(i)为用 i 种颜色的球(每种颜色必须用到)填n个盒子的种数,显然 f(1)=1 ,且有 f(i)=inC1if(1)C2if(2)Ci1if(i1) ,那么第二组盒子就是用 (mi) 种颜色的球(不要求每种颜色都用到)填入 n 个盒子的种数,即(mi)n,根据乘法原理,总的方法数就是二者的乘积。枚举第一组盒子的颜色种数就行,注意的是,当 mn 时,第一组盒子用的颜色范围为 [1,m1] ,当 m>n 时,第一组盒子用的颜色范围为 [1,n] 。组合数提前打表~

赛后想了一下其实可以用 Stirling 数来做,设第一组盒子用了 i 种颜色,那么就是将n个位置(不同),放入 i 个盒子(不同),不允许有空盒的方案数,为i!{ni},参见递推总结

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=2005;
const int MOD=1e9+7;
LL C[MAXN][MAXN];//组合数
LL f[MAXN];//f[i]表示用i种颜色填n个格子的种数

LL Q_pow(LL x,LL n)
{
    LL res=1;
    while(n)
    {
        if(n&1) res=res*x%MOD;
        n>>=1;
        x=x*x%MOD;
    }
    return res;
}

void init()
{
    for(int i=1;i<=2001;i++)
    {
        C[i][0]=C[i][i]=1;
        for(int j=1;j<i;j++)
        {
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;
        }
    }
}

void calc(int n)
{
    f[1]=1;
    for(int i=2;i<=n;i++)
    {
        LL tmp=0;
        for(int j=1;j<i;j++)
        {
            tmp=(tmp+C[i][j]*f[j]%MOD)%MOD;
        }
        f[i]=(Q_pow(i,n)-tmp+MOD)%MOD;
    }
}

int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        calc(n);
        LL ans=0;
        if(n>=m)
        {
            for(int i=1;i<m;i++)
            {
                ans=(ans+C[m][i]*f[i]%MOD*Q_pow(m-i,n)%MOD)%MOD;
            }
        }else//n<m
        {
            for(int i=1;i<=n;i++)
            {
                ans=(ans+C[m][i]*f[i]%MOD*Q_pow(m-i,n)%MOD)%MOD;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Stirling 数做法:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=2005;
const int MOD=1e9+7;
LL C[MAXN][MAXN];//组合数
LL S[MAXN][MAXN];//S[n][r] 第二类斯特林数
LL A[MAXN];//阶乘

LL Q_pow(LL x,LL n)
{
    LL res=1;
    while(n)
    {
        if(n&1) res=res*x%MOD;
        n>>=1;
        x=x*x%MOD;
    }
    return res;
}

void init()
{
    for(int i=1;i<=2001;i++)
    {
        C[i][0]=C[i][i]=1;
        for(int j=1;j<i;j++)
        {
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;
        }
    }
    A[1]=1;
    for(int i=2;i<=2001;i++)
        A[i]=i*A[i-1]%MOD;
    for(int i=1;i<=2001;i++)
    {
        S[i][0]=0;S[i][1]=1;
        for(int j=2;j<=i;j++)
        {
            S[i][j]=(j*S[i-1][j]%MOD+S[i-1][j-1])%MOD;
        }
    }
}

int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        LL ans=0;
        if(n>=m)
        {
            for(int i=1;i<m;i++)
            {
                ans=(ans+C[m][i]*A[i]%MOD*S[n][i]%MOD*Q_pow(m-i,n)%MOD)%MOD;
            }
        }else//n<m
        {
            for(int i=1;i<=n;i++)
            {
                ans=(ans+C[m][i]*A[i]%MOD*S[n][i]%MOD*Q_pow(m-i,n)%MOD)%MOD;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值