Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in the wars against the Jin dynasty of northern China. Yue Fei achieved a lot of victory and hopefully could retake Kaifeng ,the former capital of Song occupied by Jin. Fearing that retaking Kaifeng might cause the Jin to release former Emperor Song Qinzong, threatening his throne, Emperor Song Gaozong took some corrupted officers' advice, sending 12 urgent orders in the form of 12 gold plaques to Yue Fei, recalling him back to the capital.

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的更多相关文章

  1. 动态规划(计数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 ...

  2. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  3. HDU 5136 Yue Fei's Battle

    题目链接:HDU-5136 网上的一篇题解非常好,所以就直接转载了.转自oilover的博客 代码: #include<cstring> #include<cstdio> #i ...

  4. Yue Fei's Battle(组合计数递推)

    //求一个直径为 k 的树有多少种形态,每个点的度不超过 3 // 非常完美的分析,学到了,就是要细细推,并且写的时候要细心 还有除法取模需要用逆元 #include <iostream> ...

  5. HDU - 5136 2014icpc南京现场赛J 计数dp

    题目大意:给你一个树的直径k,要求每个点的度数不超过3, 问你有多少棵树满足条件. 思路:好难啊. 主要思想就是将一棵无根二叉树树划分成有根二叉树. 我们对k的分奇偶讨论: 我们定义dp[ i ] 为 ...

  6. 最大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 ...

  7. 2014ACM/ICPC亚洲区广州站题解

    这一场各种计算几何,统统没有做. HDU 5129 Yong Zheng's Death HDU 5136 Yue Fei's Battle

  8. 2017多校第7场 HDU 6121 Build a tree K叉树,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:一个n个点的完全k叉树,求每个节点的size的异或和. 解法:容易发现,考虑根的所有孩子, ...

  9. HDU 5145 NPY and girls (莫队分块离线)

    题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...

随机推荐

  1. JavaSE复习~基本语法规则

    java程序的基本结构 java程序的文件后缀为 .java 一个java文件中可以写许多内容,但有且只能有一个 public 修饰的 class 类(class)是Java程序中所有源代码的基本组成 ...

  2. Codeforces Round #621 (Div. 1 + Div. 2) C. Cow and Message

    Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is ...

  3. Java IO流详解(三)——字节流InputStream和OutPutStream

    我们都知道在计算机中,无论是文本.图片.音频还是视频,所有的文件都是以二进制(字节)形式存在的,IO流中针对字节的输入输出提供了一系列的流,统称为字节流.字节流是程序中最常用的流.在JDK中,提供了两 ...

  4. iOS 开发之应用内弹出 App Store 应用界面

    在APP内给其他APP做推广,经常用到在应用内弹出应用的APP #import <StoreKit/SKStoreProductViewController.h> 设置代理:<SKS ...

  5. 自定义Redis作为Session存储服务提供

    之前看网上介绍可使用Redis自定义Session托管,使用第三方的Harbour.RedisSessionStateStore GitHub:https://github.com/TheCloudl ...

  6. php 基础 php获取前一天,前一个月,前一年的时间

    获取前一天的时间: $mytime= date("Y-m-d H:i:s", strtotime("-1 day")); $mytime=mktime(0, 0 ...

  7. python 基础之集合

    集合 s=set('chen xi') s1=['cx','ee','cx'] s2=set(s1)#set为集合 print(s2,type(s2)) s=list(s2) print(s,type ...

  8. 4-form表单的双向绑定

    概念:表单中的input框等其他标签,值变化时会触发函数,改变state中的值,反过来修改state中的值也会改变input框中值的展现 实现:利用类组件里的state属性来实现(setState会再 ...

  9. Servlet 学习(七)

     ServletConfig 1.定义 ServletConfig接口:servlet容器在初始化期间将信息传递给servlet的servlet配置对象 代表当前Servlet在web.xml中的配置 ...

  10. java代理模式的实现方法

    package com.sample.sping_ireport.cglib; import java.lang.reflect.InvocationHandler; import java.lang ...