HDU 5136 Yue Fei ' s Battle (count DP)

Source: Internet
Author: User

Yue Fei ' s BattleTime limit:2000/1000 MS (java/others) Memory limit:512000/512000 K (java/others)
Total submission (s): 151 Accepted Submission (s): 48


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 Input
340

Sample Output
23


Test instructions: give you a number k and ask you to construct a tree with a diameter of k that is structurally different from each other

Number? (up to 3 edges per point on the tree).

Ideas:

T [i] indicates that there is an I point in the graph below, the node dp[ i] indicates that there is an I point in the graph below when, in the node                

The number of cases on which other points are added.               (For T [3]). The number of cases on which other points are added. (For DP [4])


sum[i] is the prefix and of the t[I].


Take T [3] For example:

Such as:

Because the maximum is 3, the number of points added at one point cannot exceed 2.

When 2 nodes are added on the 3rd node, a the number of cases is t[2] * (t[2]+1)/2.

When adding 1 or 0, the number of cases is t[2] * sum[3-2].

i.e. t[3] = t[2] * (t[2]+1)/2 + t[2] * sum[3-2] .

Similarly can be introduced t[i] = t [i-1] * (t[i-1] + 1)/2+t[i-1]*sum[i-2];

for Dp[i];

When I is an even number: only half is considered dp[i]=t [i/2]* (t [I/2]+1)/2;

When I is odd:

When the middle point is less than or equal to i/2-1 points, the number of cases is sum[i/2-1] * t [i/2]* (t [I/2]+1)/2;

If the I/2 points are added in the middle, then there are 3 kinds of situations to discuss.


1.3 main chains look different. So that's  t [I/2] * ( t [I/2] -1) * (  t [I/2] -2), there are 6 repetition cases.
2.2 main chain looks the same as the third one, then it is t [I/2] * (  t [I/2] -1) , no duplicates.
3.3 main chains are the same, then  t [I/2] .


i.e. dp[i]=sum[i/2-1] * t [i/2]* (t [I/2]+1)/2 + t [I/2] + t [I/2] * ( t [ I/2] -1) + t [I/2]* ( t [I/2]-1) * ( t [ I/2]-2)/6;

(The calculation process takes into account the modulus, especially the division, which requires the inverse of the element)


#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define LL Long Longusing namespace Std;const LL mod=1000000007;const int maxn=100010; LL Dp[maxn],sum[maxn],t[maxn],a,b;int K;    ll Pow_mod (ll Num,ll Coun) {ll p=num,q=coun,ret=1;        while (q) {if (Q & 1) Ret=ret*p%mod;        q>>=1;    P=p*p%mod; } return ret%mod;}    void Initial () {A=pow_mod (LL) 2,mod-2);    B=pow_mod (LL) 6,mod-2);    t[0]=1,t[1]=1,t[2]=2;    sum[0]=1,sum[1]=2,sum[2]=4;        for (int i=3;i<maxn;i++) {t[i]= (t[i-1]* (t[i-1]+1)%mod*a%mod+sum[i-2]*t[i-1]%mod)%mod;    Sum[i]= (Sum[i-1]+t[i])%mod;    } dp[1]=1,dp[2]=1;         for (int i=3;i<maxn;i++) {LL TMP=T[I/2];         LL res=tmp* (tmp+1)%mod*a%mod;         if (i%2==0) dp[i]=res;    Else dp[i]= (sum[i/2-1]*res%mod+tmp+tmp* (tmp-1+mod)%mod+tmp* (tmp-1+mod)%mod* (tmp-2+mod)%mod*b%mod)%mod;    }}int Main () {initial ();        while (scanf ("%d", &k)!=eof) {if (k==0) break;    printf ("%i64d\n", Dp[k]); } return 0;}


HDU 5136 Yue Fei ' s Battle (count 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.