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);
    }
}
發佈了55 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章