[hdu5136] Yue Fei ' s Battle 2014 Asia Regional Tournament Guangzhou Division J (DP)

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud

There was a little bit of a problem in the field because there was no successful AC, which led to a missed gold medal.

Because this afternoon something, can't reproduce, so the afternoon just spent 10 minutes to do a J question, robbed a fb,2333333333

Yue Fei ' s Battle

Time limit:2000/1000 MS (java/others) Memory limit:512000/512000 K (java/others)
Total submission (s): Accepted submission (s): 3


Problem Descriptionyue 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. 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 urgent orders in the form of a gold plaques to Yue Fei, rec Alling him back to the capital.

Then Yue Fei were put into prison and were killed under a charge of "maybe there is" treason. But later Yue Fei is 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 June. 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 has been cursed, spat and urinated upon by people. (now, don ' t do so if you go to Hangzhou and see the statues.)

One of the most important battle Yue Fei won are the battle in Zhuxian town. In the Zhuxian town, the Yue Fei wanted to the deploy some barracks, and connected those barracks with roads. Yue Fei needed all the barracks to being connected, and in order to save money, he wanted to build as less roads as possible. There couldn ' t be a barrack which are too important, or else it would be attacked by enemies. So Yue Fei required this NO barrack could connect with more than 3 roads. According to He battle theory, Yue Fei also required that the length of the longest route among the barracks is exactly K . Note the length of a route is defined as the number of barracks lied on it and there could be several longest routes WI Th the same length K.

Yue Fei wanted to know, in how many different ways could he deploy the barracks and roads. All barracks could is considered as no different. Yue Fei could deploy as many barracks as he wanted.

For example, if K are 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 are 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, the history of the change.

Inputthe input consists of no more than test cases.

For each test, there was 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 Input340

Sample Output23

Test instructions: A maximum of 3 points per node, with several nodes constituting a tree, there are several non-isomorphic structures when the diameter of the tree is K.

Analysis: Due to the need to consider the non-isomorphic situation is more complex, then we can consider to take apart the tree, first look at even, even more simple

1. If the diameter of n is even, then select the middle edge of the diameter of the tree, disconnect it, resulting in a length of N/2 two branches, in order to ensure that the count will not be isomorphic to the repetition, according to combinatorial mathematics, the scheme number is C (num[n/2]+2-1,2);

Note: Num[i] represents a non-isomorphic number of branch lengths of I.

Dp[i] represents the number of all non-isomorphic branching schemes with a branch length from 0 to I.

2. If the diameter of n is odd, select the midpoint on the diameter of the tree and divide it into a length of (n-1)/2, a length of (n-1)/2, and another 2 branch with a length of 0 to (n-1)/three.

A. If the remaining branch length is [0, (n-1)/2-1], according to combinatorial mathematics, the corresponding scheme number can be obtained dp[(n-1)/2-1]*c (num[(n-1)/2]+2-1,2);

B. If the length of the remaining branch is also (n-1)/2, then the corresponding program number in this case is C (num[(n-1)/2]+3-1,3);

That is, the odd case is dp[(n-1)/2-1]*c (num[(n-1)/2]+2-1,2) +c (num[(n-1)/2]+3-1,3);

Next consider the question of how num[i] and Dp[i] are drawn:

Obviously Dp[i] is just a prefix and, after finding out num[i], accumulate.

For Num[i], for branches with length I, only two sub-branches can be connected to the end of the branch (because the other one is to be kept connected to other branches), then for branches of length i+1, only at the end of the branch of length I, add a node, and then the new endpoint adds a branch of length 0 to I.

A. If the length of the added branch is 0 to i-1, the scheme number is num[i]*dp[i-1];

B. If the length of the added branch is I, then the scheme number is C (num[i]+2-1,2);

namely 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>Ten#include <queue> One#include <deque> A#include <stack> -#include <Set> -#include <map> the#include <cstdio> -#include <cstdlib> -#include <cmath> -#include <cstring> +#include <climits> -#include <cctype> + using namespacestd; A #defineXinf Int_max at #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) - #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) - #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) - #defineDEP (x,r,l) for (int x=r; x>=l; x--) in #defineCLR (a,x) memset (a,x,sizeof (A)) - #defineIT iterator totypedefLong Longll; +typedef pair<int,int>PII; -typedef vector<pii>VII; thetypedef vector<int>VI; * Const intmaxn=100010; $ ll DP[MAXN];Panax Notoginseng ll NUM[MAXN]; - Const intMod=1000000007; theLL INV (intx) + { A     intn=mod-2; thell temp=x; +ll ret=1; -      while(n) $     { $         if(n&1) ret=ret*temp%MoD; -temp=temp*temp%MoD; -n>>=1; the     } -     returnret;Wuyi } the intMain () - { WuIos::sync_with_stdio (false); - ll Inv_2,inv_3; AboutINV_2=INV (2); $INV_3=INV (3); -dp[0]=num[0]=1; -dp[1]=num[1]=1; -      for(intI=2; i<maxn;i++) A     { +Dp[i]= (num[i-1]*dp[i-2]%mod+ (num[i-1]+1) *num[i-1]%MOD*INV_2%MOD)%MoD; thedp[i-1]+=dp[i-2]; -dp[i-1]%=MoD; $num[i]=Dp[i]; the     } the     intN; the      while(cin>>n&&N) the     { - ll ans; in         if(n==1) ans=1; the         Else if(n==2) ans=1; the         Else if(n&1) About         { theans=num[n/2]* (num[n/2]+1)%mod*inv_2%mod*dp[n/2-1]%MoD; theans+=num[n/2]* (num[n/2]+1)%mod* (num[n/2]+2)%mod*inv_2%mod*inv_3%MoD; theans%=MoD; +         } -         Else  the         {BayiAns= (num[n/2]* (num[n/2]+1))%mod*inv_2%MoD; the         } thecout<<ans<<Endl; -     } -     return 0; the}
code June

[hdu5136] Yue Fei ' s Battle 2014 Asia Regional Tournament Guangzhou Division J (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.