HDU4903The only survival(clj计数问题ppt)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4903
题意:问有多少种无向完全图满足1-n最短路径为k且每条边都在[1,L];
L< 109 ,n,k<=12;
分析:先想暴力怎么计算答案,一种方法是枚举1到每个点的最短路长度,然后图的种数就可以计算了;仔细思考发现我们只关心d[i]=x的点个数即可,由于我们只关心d[i]

#include<bits/stdc++.h>
using namespace std;
typedef long long Int;
const int M=1e9+7;
int c[20][20];
void cal()
{
    for(int i=0;i<13;i++)c[i][0]=1;
    for(int i=1;i<13;i++)
        for(int j=1;j<=i;j++)
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%M;
}
int cnt[14];
int n,k,L,ans;
int powmod(int x,int y){int ret=1;while(y){if(y&1)ret=ret*(Int)x%M;y>>=1;x=x*(Int)x%M;}return ret;}
inline void up(int &x,int y){x+=y;if(x>=M)x-=M;}
int solve(int x)
{
    if(!cnt[x])return 1;
    int t1=1,t2=1;
    for(int j=0;j<x;j++)
    {
        if(!cnt[j])continue;
        if(x-j>L)return 0;
        t1=t1*(Int)powmod(L-(x-j)+1,cnt[j])%M;
        t2=t2*(Int)powmod(L-(x-j),cnt[j])%M;
    }
    if(x==k+1)return powmod(t1,cnt[x]);
    t1-=t2;if(t1<0)t1+=M;
    t1=powmod(t1,cnt[x]);
    return t1;
}
void dfs(int cur,int rep,int tot)
{
    if(cur==k)
    {
        for(int i=1;i+tot<=n;i++)//i个人选k
        {
            int tprep=rep*(Int)c[n-tot-1][i-1]%M;
            tprep=tprep*(Int)powmod(L,c[i][2])%M*(Int)powmod(L,c[n-tot-i][2])%M;
            cnt[k]=i;
            cnt[k+1]=n-tot-i;
            tprep=tprep*(Int)solve(k)%M*(Int)solve(k+1)%M;
            up(ans,tprep);
        }
        return;
    }
    for(int i=0;i+tot<n;i++)
    {
        cnt[cur]=i;
        int tprep=rep*(Int)powmod(L,c[i][2])%M;
        tprep=tprep*(Int)c[n-tot-1][i]%M;
        tprep=tprep*(Int)solve(cur)%M;
        dfs(cur+1,tprep,tot+i);
    }
}
int main()
{
    cal();
    int _;scanf("%d",&_);
    while(_--)
    {
        scanf("%d%d%d",&n,&k,&L);
        memset(cnt,0,sizeof(cnt));
        cnt[0]=1;
        ans=0;
        dfs(1,1,1);
        printf("%d\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值