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. 前缀和-Big Water Problem (牛客)

    链接:https://ac.nowcoder.com/acm/problem/15164 题目描述 给一个数列,会有多次询问,对于每一次询问,会有两种操作: 1:给定两个整数x, y, 然后在原数组的 ...

  2. 「Luogu P5080 Tweetuzki 爱序列」

    题目大意 给出一些数,需要求出 \(\frac{a_{i+1}}{3}=a_i\) 或 \(a_{i+1}=2 \times a_i\) 时最长的序列 \(a\). 分析 可以发现符合条件的序列 \( ...

  3. shell脚本部署apache并能通过浏览器访问!

    第一步:导入httpd-2.2.17.tar包 第二步:创建一个test.sh文件(可在/root下) 第三步编写shell脚本 > 会重写文件,如果文件里面有内容会覆盖 >>这个是 ...

  4. 【已解决】iOS11使用MJRefresh上拉加载结束tableView闪动、跳动的问题

    更新提示: [2018年11月20日更新] 经过放置在项目中运行发现,如果在快速滚动tableview的时候会在下面这行代码中崩溃(慢慢的滚动是没关系的-): CGFloat cellHeight = ...

  5. 函数返回值retrun

    如果函数不写retrun,默认返回None. return多个对象,那么Python帮我们把这多个对象封装成一个元组返回. return   作用   结束函数.返回某个对象

  6. 2020.02.28 Linux 命令

    Cat   语法格式 cat [-AbeEnstTuv] [--help] [--version] fileName 参数说明: -n 或 --number:由 1 开始对所有输出的行数编号. -b ...

  7. 十五 OGNL的入门

    一.访问对象的方法

  8. HDU1176免费馅饼(DP)

    都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内.馅饼如果掉在了地上当然就 ...

  9. idea中scala语言自动补全变量的同时,也自动补全类型

    IDE是IDEA,scala中,在new一个对象时,通过快捷键ctrl + Alt + V自动补全变量,但是我还想自动补全变量的类型,就像图中所示,在Specify type前面自动帮你打勾. 可以按 ...

  10. 6(计算机网络) 交换机与VLAN

    拓扑结构是怎么形成的? 我们常见到的办公室大多是一排排的桌子,每个桌子都有网口,一排十几个座位就有十几个网口,一个楼层就会有几十个甚至上百个网口.如果算上所有楼层,这个场景自然比你宿舍里的复杂多了.具 ...