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值作为第二关键字进行排序. 都是 ...
随机推荐
- 使用Dotfunsctor
设置Disable Control Flow.Disable Renaming.Disable String Encryption 为no,no为开启该功能 设置加密后输出的路i经 选择需要加密的ex ...
- WLC RTU license
目前思科的某些WLC不是一定要license文件去安装,例如这里提到的RTU license. RTU:Right To Use Right to Use (RTU) licensing is a m ...
- query_phase_execution_exception
ES报错信息: { "error": { "root_cause": [ { "type": "query_phase_execu ...
- 根据class 属性判断所有的文本框必填
<body> <!-- 遮罩层 --> <div id="hidediv" style="width: 100%;height: 100%; ...
- vue 组件,以及组件的复用
有时候代码的某一模块可能会经常使用到,那么完全可以把这一模块抽取出来,封装为一个组件,哪里需要用到的时候只需把模块调用即可 .参考vue官方 https://cn.vuejs.org/v2/guide ...
- Java直通车——类与对象篇
一.面向对象 教师节来临,一群小学生应召出黑板报,该怎样完成呢?对于一个小孩子来说,你告诉他:“我们要确定黑板报主题.明确内容要旨.搜寻具体内容.构思版面布局.画图和写字.安排人员后勤辅助.”他可能会 ...
- Tensorflow机器学习入门——AttributeError: module 'scipy.misc' has no attribute 'toimage'
这个bug的解决办法: import cv2 # scipy.misc.toimage(image_array).save('cifar10_data/raw/%d.jpg' % i) cv2.imw ...
- elk单机安装部署
es 下载地址:wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.1.0-linux-x86_64.t ...
- 3_06_MSSQL课程_Ado.Net_接口、委托、事件、观察者模式
1.接口——实现接口 2.委托.事件(定义事件.注册事件.触发事件) 3.接口和事件的区别,怎么分情况用? 4.观察者模式作为设计模式的一种,也称发布订阅模式. 应对类型的变化和个数的变化. 中介设计 ...
- Django问题 Did you rename .....a ForeignKey
给新加入的字段添加一个default默认值即可,让字段非空.然后在进行makemigrations,完成操作后删除相关默认值即可.