hdu 6143 Killer Names(组合计数)(DP)

题目链接:Killer Names

题意

要求构造若干名字,名字包括first name last name 两部分,均需包含 n 个字符,已知有 m 种字符供选择,求最多有多少种不同的构造方法,使得 first namelast name 不含相同字符。

也就相当于m 种颜色需要为两段长度为 n 的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况。

思路

设这两段分配的颜色数目分别为 i,j ,若在第一段总共有 (mi) 种选取方案,则在第二段总共有 (mij) 种选取方案

而在每段内部,设 f(i) 为长度为n的格子使用 i 种颜色染色的方案数

则有f(i)=inC1if(1)C2if(2)...Ci1if(i1)

此时第二段则用 mi 种颜色染色,方案数为 (mi)n 。则总的方案数就是二者的乘积。

因此最终的结果便是 m1i=1Cimf(i)(mi)n

代码:

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL mod=1e9+7;
const int maxn=2e3+5;
const int N=2e3;
LL C[maxn][maxn],f[maxn];//f[]表示用i种颜色填n个格子的种数

LL qmod(LL x,int n)
{
    LL res=1;
    while(n)
    {
        if(n&1)
            res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}

void init()//组合数
{
    for(int i=1; i<=N; ++i)
    {
        C[i][0]=C[i][i]=1;
        for(LL j=1; j<i; ++j)
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%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]=(qmod(i,n)-tmp+mod)%mod;
    }
}

int main()
{
    init();
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        calc(n);
        LL ans=0;
        for(int i=1; i<=n&&i<m; ++i)
            ans=(ans+(C[m][i]*f[i]%mod)*qmod(m-i,n)%mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}



也可以使用 Stirling 数

S(p,k) 的组合学解释是:将 p 个物体划分成k个非空的无区别的集合的方案数。

在题目中就是,将 n 个位置(不同),放入m个盒子(不同),不允许有空盒的方案数

即结果为 m!×S(n,m)

代码:

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL mod=1e9+7;
const int maxn=2e3+5;
const int N=2e3;
LL C[maxn][maxn],S[maxn][maxn];//S[][]第二类斯特林数
LL A[maxn];//阶乘

LL qmod(LL x,int n)
{
    LL res=1;
    while(n)
    {
        if(n&1)
            res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}

void init()
{
    for(int i=1; i<=N; ++i)
    {
        C[i][0]=C[i][i]=1;
        for(int j=1; j<i; ++j)
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    A[1]=1;
    for(int i=2; i<=N; ++i)
        A[i]=i*A[i-1]%mod;
    for(int i=1; i<=N; ++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,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        LL ans=0;
        for(int i=1; i<=n&&i<m; ++i)
            ans=(ans+((C[m][i]*A[i]%mod)*S[n][i]%mod)*qmod(m-i,n)%mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}



DP也可以做

dp[i][j] 表示长度为 i 的格子用了j种颜色染色的方案数

dp[i][j]=(dp[i1][j1](mj+1)%mod+dp[i1][j]j%mod)%mod

代码:

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL mod=1e9+7;
const int maxn=2e3+5;
const int N=2e3;
LL dp[maxn][maxn];

LL qmod(LL x,int n)
{
    LL res=1;
    while(n)
    {
        if(n&1)
            res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}

int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        dp[1][1]=m;
        for(int i=2; i<=n; ++i)
            for(int j=1; j<=i&&j<m; ++j)
                dp[i][j]=(dp[i-1][j-1]*(m-j+1)%mod+dp[i-1][j]*j%mod)%mod;
        LL ans=0;
        for(int i=1; i<m&&i<=n; ++i)
            ans=(ans+dp[n][i]*qmod(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、付费专栏及课程。

余额充值