Day9 - K - Yue Fei's Battle HDU - 5136
Then Yue Fei was put into prison and was killed under a charge of "maybe there is" treason. But later Yue Fei was posthumously pardoned and rehabilitated, and became a symbol of loyalty to the country. The four corrupted officers who set him up were Qin Hui,Qin Hui's wife Lady Wang, Moqi Xie and Zhang Jun. People made kneeling iron statues of them and put the statues before Yue Fei's tomb (located by the West Lake, Hangzhou). For centuries, these statues have been cursed, spat and urinated upon by people. (Now please don't do that if you go to Hangzhou and see the statues.)
One of the most important battle Yue Fei won is the battle in Zhuxian town. In Zhuxian town, Yue Fei wanted to deploy some barracks, and connected those barracks with roads. Yue Fei needed all the barracks to be connected, and in order to save money, he wanted to build as less roads as possible. There couldn't be a barrack which is too important, or else it would be attacked by enemies. So Yue Fei required that NO barrack could connect with more than 3 roads. According to his battle theory, Yue Fei also required that the length of the longest route among the barracks is exactly K. Note that the length of a route is defined as the number of barracks lied on it and there may be several longest routes with the same length K.
Yue Fei wanted to know, in how many different ways could he deploy the barracks and roads. All barracks could be considered as no different. Yue Fei could deploy as many barracks as he wanted.
For example, if K is 3,Yue Fei had 2 ways to deploy the barracks and roads as shown in figure1. If K is 4, the 3 kinds of layouts is shown in figure 2. (Thick dots stand for barracks, and segments stand for roads):
Please bring your computer and go back to Yue Fei's time to help him so that you may change the history.
InputThe input consists of no more than 25 test cases.
For each test, there is only one line containing a integer K(1<=K<=100,000) denoting the length of the longest route.
The input ends by K = 0.OutputFor each test case, print an integer denoting the number of different ways modulo 1000000007.Sample Input
3
4
0
Sample Output
2
3 思路:dp计数问题,要求一棵直径为k的树有多少种,每个节点最多有3个分支,可以将其分为一个深度为k/2的二叉树,设dp[i]为深度为i的二叉树不同构意义下的数目,则有2种情况,深度为i的子树深度都是i-1,若两者形态相等,就是dp[i-1],不相等,就是C(dp[i-1],2),若只有一棵是i-1,则数目为dp[i-1]*sum[i-2],sum为dp的前缀和,看统计答案时,若k为偶数,则k对半分,在中间设一个虚拟节点,两边深度都是k/2,若两者相同,则是dp[k/2],若不同,则是C(dp[k/2], 2),当k为奇数的时候,就有2种情况,选一个节点当父节点,其有2个或3个子树,且至少有2个子树的深度为k/2,当2个子树深度为k/2时,与偶数情况相同,多了一个小于k/2的子树,就是(C(dp[k/2], 2)+dp[k/2])*sum[k/2-1], 当3个子树深度都是k/2时,又有三种情况,三者形态相等,dp[k/2],两个相等,C(2,1)*C(dp[k/2],2),都不相等,C(dp[k/2],3)
typedef long long LL;
typedef pair<LL, LL> PLL; const LL MOD = 1e9+;
const LL inv2 = ;
const LL inv3 = ;
const LL inv6 = ;
const int maxm = 1e5+; LL sum[maxm], dp[maxm]; void init() {
dp[] = dp[] = sum[] = ;
dp[] = sum[] = , sum[] = ;
for(int i = ; i < maxm; ++i) {
dp[i] = (dp[i-]+)*dp[i-]%MOD*inv2%MOD;
dp[i] = (dp[i] + dp[i-]*sum[i-]%MOD)%MOD;
sum[i] = (sum[i-] + dp[i]) % MOD;
}
} void run_case(int k) {
LL ans = ;
if(k & ) {
k /= ;
ans = dp[k]*(dp[k]+)%MOD*inv2%MOD*sum[k-]%MOD;
ans = (ans + dp[k]*(dp[k]-+MOD)%MOD*(dp[k]-+MOD)%MOD*inv6%MOD)%MOD;
ans = (ans + dp[k]*(dp[k]-+MOD)%MOD+dp[k])%MOD;
} else {
k /= ;
ans = dp[k]*(dp[k]+)%MOD*inv2%MOD;
}
cout << ans << endl;
} int main() {
ios::sync_with_stdio(false), cin.tie();
init();
int k;
while(cin >> k && k)
run_case(k);
return ;
}
Day9 - K - Yue Fei's Battle HDU - 5136的更多相关文章
- 动态规划(计数DP):HDU 5136 Yue Fei's Battle
Yue Fei's Battle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Other ...
- [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...
- HDU 5136 Yue Fei's Battle
题目链接:HDU-5136 网上的一篇题解非常好,所以就直接转载了.转自oilover的博客 代码: #include<cstring> #include<cstdio> #i ...
- Yue Fei's Battle(组合计数递推)
//求一个直径为 k 的树有多少种形态,每个点的度不超过 3 // 非常完美的分析,学到了,就是要细细推,并且写的时候要细心 还有除法取模需要用逆元 #include <iostream> ...
- HDU - 5136 2014icpc南京现场赛J 计数dp
题目大意:给你一个树的直径k,要求每个点的度数不超过3, 问你有多少棵树满足条件. 思路:好难啊. 主要思想就是将一棵无根二叉树树划分成有根二叉树. 我们对k的分奇偶讨论: 我们定义dp[ i ] 为 ...
- 最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- 2014ACM/ICPC亚洲区广州站题解
这一场各种计算几何,统统没有做. HDU 5129 Yong Zheng's Death HDU 5136 Yue Fei's Battle
- 2017多校第7场 HDU 6121 Build a tree K叉树,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:一个n个点的完全k叉树,求每个节点的size的异或和. 解法:容易发现,考虑根的所有孩子, ...
- HDU 5145 NPY and girls (莫队分块离线)
题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...
随机推荐
- lena全身像
数字图像处理中,Lena(Lenna)是一张被广泛使用的标准图片,特别在图像压缩的算法研究中 (为什么用这幅图,是因为这图的各个频段的能量都很丰富:即有低频(光滑的皮肤),也有高频(帽子上的羽毛),很 ...
- ORM框架的概述
ORM: object relation mapping [对象][关系]映射 将对象 映射到 数据库中 类名 数据库表名 对象 ...
- dfs & bfs总结
dfs 最简单的三种形式递归总结 bfs 百度https://baike.baidu.com/item/%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7 ...
- MyBatis学习(五)
Spring和MyBaits整合 1.整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactor ...
- Spring学习(九)
JdbcTemplate需要的jar包 1.Spring核心必须依赖的库:commons-logging-1.1.1.jar2.Spring IoC部分核心库: spring-beans-4.3.9. ...
- spark-env.sh增加HADOOP_CONF_DIR使得spark运行文件是hdfs文件
spark-env.sh增加HADOOP_CONF_DIR使得spark读写的是hdfs文件 刚装了spark,运行wordcount程序,local方式,执行的spark-submit,读和写的文件 ...
- VUE父子组件相互传值
passer.vue中代码 首先在文件中引入组件 import canvasDraw from '@/components/CanvasDraw/canvasDraw' 局部注册组件:componen ...
- LVS负载均衡软件使用及(LVS简介、三种工作模式、十种调度算法)
一.LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案, ...
- centos610无桌面安装libreoffice缺失字体
1.安装libreoffice 2.安装fontconfig yum -y install fontconfig 3.安装ttmkfdir yum -y install ttmkfdir 4.检查已有 ...
- 代理模式之静态代理,JDK动态代理和cglib动态代理
代理模式,顾名思义,就是通过代理去完成某些功能.比如,你需要购买火车票,不想跑那么远到火车站售票窗口买,可以去附近的火车票代售点买,或者到携程等第三方网站买.这个时候,我们就把火车站叫做目标对象或者委 ...