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值作为第二关键字进行排序. 都是 ...
随机推荐
- twisted task.cpperator
twisted task.cpperator 1. twisted task.cpperator 1.1. 简介-cooperator 官方文档: https://twistedmat ...
- Docker for YApi--一键部署YApi
获取YApi镜像$ docker pull mrjin/yapi:latest 注意:本仓库目前只支持安装,暂不支持升级,请知晓.如需升级请备份mongoDB内的数据. docker-compose ...
- WLC-Download 3-party CA to WLC
一.基础准备 为了创建和导入第三方SSL-certificate你需要做如下准备:1.一个WLC(随着版本的不同,可能需要准备的也不同)这里以7.0.98版本为例.2.一个外部的证书颁发机构(Cert ...
- leetcode 0211
目录 ✅ 1217. 玩筹码 描述 解答 c java py ✅ 206. 反转链表 描述 解答 c java py ✅ 922. 按奇偶排序数组 II 描述 解答 c 双指针soldier tddo ...
- [02]Sort选择排序
选择排序 算法速度:通过大O表示法表示,O(n),n是操作数,表示算法执行的次数: 数组:是有序的元素序列:若将有限个类型相同的变量的集合命名,那么这个名称为数组名: 链表:是一种物理存储单元上非连续 ...
- idea使用小技巧
1.按住alt,鼠标往下拉一条直线,可以选中一列或多列,或者不选中任何文字,可以让光标定位到这几行的相同的列的位置,然后输入文本,发现在被选中的所有行同时输入了这些文本(类似notepad++): 2 ...
- 【PAT甲级】1050 String Subtraction (20 分)
题意: 输入两个串,长度小于10000,输出第一个串去掉第二个串含有的字符的余串. trick: ascii码为0的是NULL,减去'0','a','A',均会导致可能减成负数. AAAAAccept ...
- 设计模式六大原则——开放封闭原则(OCP)
什么是开闭原则? 定义:是说软件实体(类.模块.函数等等)应该可以扩展,但是不可修改. 开闭原则主要体现在两个方面: 1.对扩展开放,意味着有新的需求或变化时,可以对现有代码进行扩展,以适应新的情况. ...
- HTML常用标签效果展示
HTML常用标签效果展示 一.文本效果 段落1---收到了开发建设看来得更加快乐圣诞节福利肯定是减肥的路上苏里科夫就是打开了飞机都是风口浪尖上的疯狂了大煞风景圣诞快乐的索科洛夫几点上课了关键是低空掠过 ...
- springMVC是如何实现参数封装和自动返回Json的
HTTP 请求和响应是基于文本的,意味着浏览器和服务器通过交换原始文本进行通信.但是,使用 Spring,controller 类中的方法返回纯 ‘String’ 类型和域模型(或其他 Java 内建 ...