hdu 4903 The only survival
The only survival
http://acm.hdu.edu.cn/showproblem.php?pid=4903
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Something bad actually happen. The devil makes this kingdom's people infected by a disease called lolicon. Lolicon will take away people's life in silence.
Although z*p is died, his friend, y*wan is not a lolicon. Y*wan is the only one in the country who is immune of lolicon, because he like the adult one so much.
As this country is going to hell, y*wan want to save this country from lolicon, so he starts his journey.
You heard about it and want to help y*wan, but y*wan questioned your IQ, and give you a question, so you should solve it to prove your IQ is high enough.
The problem is about counting. How many undirected graphs satisfied the following constraints?
1. This graph is a complete graph of size n.
2. Every edge has integer cost from 1 to L.
3. The cost of the shortest path from 1 to n is k.
Can you solve it?
output the answer modulo 10^9+7
For each test case, the first line contains 3 integers n,k,L.
T<=5 n,k<=12,L<=10^9.
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k,l,ans;
int d[],f[],C[][];
const int mod=1e9+;
void dfs(int now,int dis,bool ok,int sum)
//当前枚举哪个点,这个点到1的最短距离,是否满足第n个点到1的最远距离为k,当前方案数
{
if(now== && !dis) return;//dis初值为0,只有第一个点的距离为0
d[now]=dis; ok|=dis==k;
if(now>)//计算当前点和前面所有点之间连边的方案数
if(dis<=k)
{
f[]=; f[]=;
//f[i][0/1]:第now与1到i之间的边是否存在一条边使当前dis成立
for(int i=;i<now;i++)
if(d[i]==dis) //1到now的最短距离==1到i的最短距离 ,那么now和i之间的边可以为任意长度,第i个点对dis是否成立毫无影响
{
f[]=1ll*f[]*l%mod;
f[]=1ll*f[]*l%mod;
}
else
// 要保证1到now的最短距离为dis,如果之前在1——i-1中,已经有一条边使dis成立,那么i与now之间的边长只需>=dis-d[i];如果1——i-1中不存在这么一条边,那么i与now之间的边=dis-d[i]
//如果1——i与now之间的边不能使dis成立,前i-1个已经考虑过了,最后一个i与now之间的边 要>dis-d[i]
{//设这条边边权为val
f[]=(1ll*f[]*(l-dis+d[i]+)%mod+f[])%mod; //d[i]+val>=dis ==> dis-d[i]<=val<=L ==> val有L-(dis-d[i])+1种选择
f[]=1ll*f[]*(l-dis+d[i])%mod; //d[i]+val>dis ==> dis-d[i]<val<=L ==> val有L-(dis-d[i])种选择
}
sum=1ll*sum*f[]%mod;
}
else for(int i=;i<now;i++) sum=1ll*sum*min(l,l-k+d[i])%mod;
//如果dis>k,那么1到n的最短路一定不经过now,now与其他点的边就无所谓了
if(now==n)
{
if(!ok) return;
int j;
//搜索的时候d按不上升搜索,所以搜索出的d还要分配给每个点,组合计算分配方案数
for(int tmp=n-,i=;i<=n;i=j)
// 除去1和n,还剩n-2个点待分配
{
for(j=i;d[i]==d[j] && j<=n;j++);
//相等的d的范围:i——j-1,所以相等的d的个数为j-i
//给tmp个点中的j-i个点分配d,方案数为C(tmp,j-i)
if(d[i]==k) i++;//如果当前d==k,那么包含了点n,需要减去,给i加1,相当于给j-i减1
sum=1ll*sum*C[tmp][j-i]%mod;
tmp-=j-i; //有j-i个点分配完了
}
ans=(ans+sum)%mod;
return;
}
for(;dis<=k+;dis++) dfs(now+,dis,ok,sum);
//边长>k且<l的边全部看为k+1
}
int main()
{
C[][]=;
for(int i=;i<=;i++)
{
C[i][]=;
for(int j=;j<=i;j++)
C[i][j]=(C[i-][j-]+C[i-][j])%mod;
}
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&k,&l);
if(k>l) { puts(""); continue; }
ans=;
dfs(,,,);
printf("%d\n",ans);
}
}
#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; typedef long long LL; const int mod=1e9+; int N,K,L; int C[][]; int d[];
int f[]; int ans; void dfs(int now,int dis,bool ok,int sum)
{
if(now== && !dis) return;
d[now]=dis;
ok|=dis==K;
if(now>)
if(dis<=K)
{
f[]=;
f[]=;
for(int i=;i<now;++i)
if(d[i]==dis)
{
f[]=(LL)f[]*L%mod;
f[]=(LL)f[]*L%mod;
}
else
{
f[]=(LL)f[]*(L-dis+d[i]+)%mod;
f[]+=f[];
f[]-=f[]>=mod ? mod : ;
f[]=(LL)f[]*(L-dis+d[i])%mod;
}
sum=(LL)sum*f[]%mod;
}
else
for(int i=;i<now;++i) sum=(LL)sum*min(L,L-K+d[i])%mod;
if(now==N)
{
if(!ok) return;
int j;
for(int tmp=N-,i=;i<=N;i=j+)
{
for(j=i;d[j]==d[i] && j<=N;++j);
j--;
int cnt=j-i+;
if(d[i]==K) cnt--;
sum=(LL)sum*C[tmp][cnt]%mod;
tmp-=cnt;
}
ans+=sum;
ans-=ans>=mod ? mod : ;
return;
}
for(;dis<=K+;++dis) dfs(now+,dis,ok,sum);
} int main()
{
C[][]=;
for(int i=;i<=;++i)
{
C[i][]=;
for(int j=;j<=i;++j)
{
C[i][j]=C[i-][j]+C[i-][j-];
C[i][j]-=C[i][j]>=mod ? mod : ;
}
}
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&N,&K,&L);
ans=;
if(K<=L) dfs(,,false,);
cout<<ans<<'\n';
}
}
hdu 4903 The only survival的更多相关文章
- HDU.4903.The only survival(组合 计数)
题目链接 惊了 \(Description\) 给定\(n,k,L\),表示,有一张\(n\)个点的无向完全图,每条边的边权在\([1,L]\)之间.求有多少张无向完全图满足,\(1\)到\(n\)的 ...
- HDOJ 4903 The only survival
Discription: There is an old country and the king fell in love with a devil. The devil always ask th ...
- HDU 4903 (模拟+贪心)
Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- HDU 3062 Party(2-SAT模版题)
Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是 ...
- 浮点数(floating-point number)二进制存储格式
定义 浮点数就是小数点位置不固定的数,也就是说与定点数不一样,浮点数的小数点后的小数位数可以是任意的,根据IEEE754-1985(也叫IEEE Standard for Binary Floatin ...
- 自测之Lesson15:TCP&UDP网络编程
题目:编写一个TCP通信的程序. 实现代码: #include <stdio.h> #include <sys/socket.h> #include <unistd.h& ...
- ACM入门步骤(一)
一般的入门顺序: 0. C语言的基本语法(或者直接开C++也行,当一个java选手可能会更受欢迎,并且以后工作好找,但是难度有点大),[参考书籍:刘汝佳的<算法竞赛入门经典>,C++入门可 ...
- lintcode-151-买卖股票的最佳时机 III
151-买卖股票的最佳时机 III 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易. 注意事项 你不可以同时参与多笔交易(你必须在 ...
- iOS- 优化与封装 APP音效的播放
1.关于音效 音效又称短音频,是一个声音文件,在应用程序中起到点缀效果,用于提升应用程序的整体用户体验. 我们手机里常见的APP几乎都少不了音效的点缀. 显示实现音效并不复杂,但对我们App很 ...
- 201621044079 week13 网络
作业13-网络 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以 ...
- 原生js操作Dom节点:CRUD
知识点,依然会遗忘.我在思考到底是什么原因.想到研究生考试准备的那段岁月,想到知识体系的建立,知识体系分为正向知识体系和逆向知识体系:正向知识体系可以理解为教科书目录,逆向知识体系可以理解考试真题. ...
- [OS] 多线程--第一次亲密接触CreateThread与_beginthreadex本质区别
转自:http://blog.csdn.net/morewindows/article/details/7421759 本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_be ...
- 【Python】python和json数据相互转换,json读取和写入,repr和eval()使用
python数据转换json 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import jso ...