HDU 5136 Yue Fei ' s Battle

Source: Internet
Author: User

Test instructions: The number of different trees with k points on the diameter (no more than 3 per point).

The binary tree satisfies each point degree not to exceed 3, the diameter cuts from the middle, both sides is two forks tree.

Set Dp[i] = The number of different binary trees with a depth of I.

Sum[i] = the number of different binary trees with a depth not exceeding I.

Then there are 3 cases of two branches of the two-fork tree:

One branch depth is i-1, another branch depth is less than i-1, there are dp[i-1] * sum[i-2] methods;

Two branch depths are i-1, two branches are different, there are dp[i-1]* (dp[i-1]-1)/2 methods;

Two branch depths are i-1, two branches, like dp[i-1] methods.

Dp[i] = dp[i-1]*sum[i-2] + dp[i-1]* (dp[i-1]+1)/2

i = K/2

When K is an even number, both sides of the two-fork tree depth are K/2,

Consider the two sides may be the same, may be different, the number of schemes is dp[i]* (dp[i]+1)/2;

When k is odd, the middle point is connected to 3 branches, at least 2 branch depths are K/2.

When 3 branches have a branch depth less than K/2, the scheme number dp[i]* (dp[i]+1)/2*sum[i-1]

When the depth of 3 branches is K/2:

When 3 branches are the same, the number of scenarios dp[i]

When 3 branches 2 are the same, the scheme number is dp[i] * (Dp[i]-1)

When 3 branches are different, the program number C (Dp[i], 3)

All things added up.

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


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

SOURCE2014ACM/ICPC Asia Guangzhou Station-re-match (thanks to Chinese laborers and PKU)
#include <bits/stdc++.h>using namespace std; #define PRT (k) cerr<< #k "=" <<k<<endltypedef long    Long ll;const ll mod = 1e9 + 7;ll pmod (ll A, ll N) {ll ret = 1;    for (; n; n>>=1, A=a*a%mod) if (n&1) ret = ret * a% mod; return ret;} ll Getinv (ll a) {return Pmod (A, mod-2);} const int N = 100100;LL dp[n + 5], Sum[n + 5];const ll inv2 = GETINV (2); ll Sub (ll A, ll b) {return (-a + MoD)% MoD;    }void init () {dp[0] = 1;    DP[1] = 1;    Sum[0] = 1;    SUM[1] = 2;        for (int i=1;i<n;i++) {dp[i+1] = (dp[i] * sum[i-1]% mod);        Dp[i+1] = (dp[i+1] + (Dp[i] + 1)%mod * dp[i]% mod * inv2% mod)% MoD;    Sum[i+1] = (Sum[i] + dp[i+1])% MoD; }}ll C3 (ll a) {return ((A*sub (a,1)%mod) * SUB (a,2)% MoD) * GETINV (6)% MoD;}    ll f (int K) {int i = K/2;    ll ans = ((dp[i] * (Dp[i] + 1)%mod)% mod * inv2% mod)% MoD;    ll tmp = ans;    if (k%2==0) {return ans;    } ans = (ans * sum[i-1])% MoD; ANS = (ans + dp[i])% MoD;    Ans = (ans + (dp[i]-1+mod)%mod * dp[i]% mod)% MoD;    Ans = (ans + C3 (dp[i]))% MoD; return ans;}    int main () {int K;    Init ();        while (Cin>>k, K) {ll ans = f (k);    cout<<ans<<endl; } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5136 Yue Fei ' s Battle

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.