Hdu 6143 Killer Names(第二类strling数)

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.

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

这道题要问的是给你m种字母,让你分成两堆,每堆有n个,每堆中可以有重复但两堆之间不能有重复,问有多少种情况。做法是先选出first name然后再选last name,首先将m分成i和(m-i)两个部分,i作为姓,(m-i)作为名,名里面的(m-i)可以随便用,种类是(m-i)^n,但名必须在n的长度上用完所有的,这样就能保证了所有的(m-i)都能对应到所有的不重复的姓的种类。而为了保证每个姓的不重复选择,我们要用到第二类strling数,S[i][n]的意思就是将i件物品分成n个集合的方法(这里有个技巧,将i件物品分成j份,如果需要可重复选择, 也就是将j份分到i件物品,所以S[i][j]==S[j][i]),而每种集合都有i!种排列方法,所以总数是:q_pow(m-i,n)*C[m][i]*S[i][n]*fac[i]

#include<bits/stdc++.h>
using namespace std;
using LL=int64_t;
const int INF=0x3f3f3f3f;
const LL mod=1e9+7;
LL n,m;
LL fac[2005],S[2005][2005],C[2005][2005];

void init() {
    fac[0]=1;
    for(int i=1;i<=2002;i++)
        fac[i]=fac[i-1]*i%mod;
    memset(S,0,sizeof(S));
    S[0][0]=1;
    for(int i=1;i<2002;i++) {
        S[i][0]=0,S[i][i]=1;
        for(int j=1;j<i;j++) {
            S[i][j]=(j*S[i-1][j]+S[i-1][j-1]+mod)%mod;
            S[j][i]=S[i][j];
        }
    }
    for(int i=0;i<2005;i++)
            C[i][0]=C[i][i]=1;
    for(int i=2;i<2005;i++)
        for(int j=1;j<i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j]+mod) % mod;
}

LL q_pow(LL x,LL n) {
    LL ans=1;
    x%=mod;
    while(n){
        if(n&1) ans=(ans*x)%mod;
        n>>=1;
        x=(x*x)%mod;
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    init();
    int T;
    cin>>T;
    while(T--) {
        LL n,m,sum=0;
        cin>>n>>m;
        for(int i=1;i<m&&i<=n;i++) {
            sum+=(q_pow(m-i,n)*C[m][i]%mod*S[i][n]%mod*fac[i]+mod)%mod;
        }
        cout<<sum%mod<<endl;
    }
    return 0;
}

这道题我刚开始的想法有点简单,我想着将m分成k和(m-k)份,然后种类就是C[m][k]* q_pow(m-k,n)*q_pow(k,n),但我后来发现在这种思路下,每个名对应的姓是有重复情况的,完全没有卵用啊。把m=4,n=2的情况全列出来就知道了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值