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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂。



由于今天下午有点事情,无法打重现,所以下午只是花了十分钟做了一道J题,抢了个FB,2333333333

Yue Fei‘s Battle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 15    Accepted Submission(s): 3

Problem Description

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.

Input

The 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.

Output

For each test case, print an integer denoting the number of different ways modulo 1000000007.

Sample Input

3

4

0

Sample Output

2

3

题意:规定每个结点最多连3个点,有若干个结点构成一棵树,则在树的直径为k时,共有几种非同构的结构。

分析:由于需要考虑非同构的情况比较复杂,那么,我们可以考虑将这棵树拆开,先看偶数,偶数比较简单

1.若直径n为偶数,则选取这棵树的直径的中间那条边,将其断开,从而产生长度均为n/2的两个分支,为了保证计数时不会将同构的重复计入,根据组合数学,则方案数为C(num[n/2]+2-1,2);

注:num[i]表示分支长度为i的非同构的种数。

  dp[i]表示分支长度从0到i的所有非同构的分支方案数。

2.若直径n为奇数,则选取这棵树的直径上的中点,从而拆分成长度为(n-1)/2,长度为(n-1)/2以及另一条长度可为0到(n-1)/2的三个分支。

  a.若剩下一条分支的长度为[0,(n-1)/2-1],根据组合数学,可以得到对应的方案数为dp[(n-1)/2-1]*C(num[(n-1)/2]+2-1,2);

  b.若剩下另一条分支的长度也为(n-1)/2,则此情况下对应的方案数为C(num[(n-1)/2]+3-1,3);

  即奇数情况为dp[(n-1)/2-1]*C(num[(n-1)/2]+2-1,2)+C(num[(n-1)/2]+3-1,3);

接下来考虑num[i]与dp[i]是如何得出的问题:

显然dp[i]只是一个前缀和,求出num[i]之后累加即可。

对于num[i],对于长度为i的分支,其端点上只能连接两个子分支(因为另一个要留着与其它分支相连),那么,对于长度为i+1的分支,则只是在长度为i的分支的端点处,增加了一个结点,然后再在新的端点上添加长度为0到i的分支。

  a.若添加的分支的长度为0到i-1,则方案数为num[i]*dp[i-1];

  b.若添加的分支的长度为i,则方案数为C(num[i]+2-1,2);

  即num[i+1]=num[i]*dp[i-1]+C(num[i]+2-1,2);

 1 #include <iostream>
 2 #include <sstream>
 3 #include <ios>
 4 #include <iomanip>
 5 #include <functional>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <string>
 9 #include <list>
10 #include <queue>
11 #include <deque>
12 #include <stack>
13 #include <set>
14 #include <map>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cmath>
18 #include <cstring>
19 #include <climits>
20 #include <cctype>
21 using namespace std;
22 #define XINF INT_MAX
23 #define INF 0x3FFFFFFF
24 #define MP(X,Y) make_pair(X,Y)
25 #define PB(X) push_back(X)
26 #define REP(X,N) for(int X=0;X<N;X++)
27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
29 #define CLR(A,X) memset(A,X,sizeof(A))
30 #define IT iterator
31 typedef long long ll;
32 typedef pair<int,int> PII;
33 typedef vector<PII> VII;
34 typedef vector<int> VI;
35 const int maxn=100010;
36 ll dp[maxn];
37 ll num[maxn];
38 const int mod=1000000007;
39 ll inv(int x)
40 {
41     int n=mod-2;
42     ll temp=x;
43     ll ret=1;
44     while(n)
45     {
46         if(n&1)ret=ret*temp%mod;
47         temp=temp*temp%mod;
48         n>>=1;
49     }
50     return ret;
51 }
52 int main()
53 {
54     ios::sync_with_stdio(false);
55     ll inv_2,inv_3;
56     inv_2=inv(2);
57     inv_3=inv(3);
58     dp[0]=num[0]=1;
59     dp[1]=num[1]=1;
60     for(int i=2;i<maxn;i++)
61     {
62         dp[i]=(num[i-1]*dp[i-2]%mod+(num[i-1]+1)*num[i-1]%mod*inv_2%mod)%mod;
63         dp[i-1]+=dp[i-2];
64         dp[i-1]%=mod;
65         num[i]=dp[i];
66     }
67     int n;
68     while(cin>>n&&n)
69     {
70         ll ans;
71         if(n==1)ans=1;
72         else if(n==2)ans=1;
73         else if(n&1)
74         {
75             ans=num[n/2]*(num[n/2]+1)%mod*inv_2%mod*dp[n/2-1]%mod;
76             ans+=num[n/2]*(num[n/2]+1)%mod*(num[n/2]+2)%mod*inv_2%mod*inv_3%mod;
77             ans%=mod;
78         }
79         else
80         {
81             ans=(num[n/2]*(num[n/2]+1))%mod*inv_2%mod;
82         }
83         cout<<ans<<endl;
84     }
85     return 0;
86 }

代码君

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

时间: 2024-08-02 15:10:52

[hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)的相关文章

hdu 5024 DFS 2014亚洲区域赛广州网赛

http://acm.hdu.edu.cn/showproblem.php?pid=5024 DFS写的好像质量不太高,昨晚T了,然后睡的时候重新理下思路,今天重写下,750MSAC,太慢了 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algorithm> #include <stri

ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)

题目地址:点击打开链接 这道题有两种做法,第一种是直接求期望,类似于poj 2096 区别在于这个步数有限.所以要迭代步数. #include <cstdio> #include <cstring> #include <iostream> #define maxn 55//这里刚开始写成了50+10 那么maxn*maxn就会小很多wa了一次 using namespace std; double dp[maxn][maxn][maxn*maxn]; int N,M,T

08年acm区域赛北京赛区 部分题解题报告

08年区域赛北京赛区 http://poj.org/searchproblem?field=source&key=Beijing+2008 POJ 3921 Destroying the bus stations 题目还是比较难的,当时的榜似乎只有4/25的通过/提交,其实题目数据很水.学长转换模型写了网络流求最小割,可以AC,不过自己造了个数据推翻了正确性.我写了个很挫的bfs套bfs,外层是最小的删除点数,内层是求最短路,数据很水可以AC.但比较蛋疼的在于bfs耗内存,而且队列中的点数是阶乘

2015亚洲区域赛长春赛区网络预选赛

第一次打网络赛,第一场,总体来说还可以吧,但是我们队三个人状态都并不太好,主要就是 WA 的比较多吧,开场看最后一题是我的习惯了,虽然貌似那题到打了一半可能才有队伍做出来了,我看了感觉像前几天训练赛的时候做的一道题.刻盘开场看 06 ,凯神开场 01.接着两分钟学长发现 07 水题我就跟着看了发题意,求区间最大值,静态.然后数据范围也很小,就直接开敲暴力,四分钟的时候过的,大概 20 名左右吧,那就是我们的最高名次了2333……接着我准备继续研究下 13 ,刻盘告诉我 06 是关于循环的子串的问

ZOJ3819 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江司A称号 Average Score 注册标题

Average Score Time Limit: 2 Seconds      Memory Limit: 131072 KB Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mathematical Analysis. After a mid-term exam, Bob was anxious about his

第39届ACM亚洲区域赛牡丹江赛区赛后总结

2014年10月10日,周五,早晨匆匆忙忙的出了寝室,直奔复印社去打了两份模板,然后直接就去上课了.第三节课下课,直接跟老师讲了一声,就去实验室跟学长们汇合了.12点半,踏上了开往牡丹江的列车,我们那节车厢俨然成了参赛队伍的专列了,背后坐的是吉大的,学生还没咋看,主要都看他们教练了,那肚子挺得,那得多腐败啊...旁边的是华南理工的,没想到旁边的那个妹纸,竟然是他们的教练!!!看着就是学生呀,竟然已经留校当教练了,唉,好可惜(你们懂得,嘿嘿~~~) 抵达牡丹江,这是我第二次踏上这块土地,当时想着这

2013 ACM-ICPC亚洲区域赛南京站C题 题解 轮廓线DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4804 题目大意 给你一个 \(n \times m\) 的矩形区域.你需要用 \(1 \times 1\) 和 \(1 \times 2\) 的砖块铺满这个区域,且满足如下要求: 所有的砖块可以竖着放或横着放: 砖角要放在格点上: \(1 \times 1\) 的砖不能少于 \(C\) 块也不能多于 \(D\) 块, \(1 \times 2\) 的砖没有数量限制. 有些方格在一开始就已经被填充了,

2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)

http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只能消灭一个敌人.现在有药剂,英雄喝了之后可以多消灭一个敌人,但每个英雄只能喝一瓶,问最多能消灭多少个敌人. 下午在实验室队内自己开训练,和JC大佬那队一起开的,当时JC大佬他们队开的J题,没有看I题,当我们队AC之后JC大佬才看了I题,听到他们说,这题就差直接把网络流三个字写在题目里了.确实非常明显

Yue Fei&#39;s Battle(组合计数递推)

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