您的位置:首页 > 其它

HDU4903The only survival(clj计数问题ppt)

2015-10-08 23:30 162 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4903

题意:问有多少种无向完全图满足1-n最短路径为k且每条边都在[1,L];

L<10910^9,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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: