Topcoder SRM 666 DIV 1

Source: Internet
Author: User

Walkoveratree

Test instructions: Give you a tree, there is a person in node 0, now ask you, this person walk l step, up to how many different nodes, a node can be walked several times, but only one time.

The crux of the problem is that each point is up to two times, because I can either go to this point at one time, or go back from this point to the other points, there is no way to walk three times, it needs to be carefully thought out.

So we can get an algorithm that enumerates a one-off path and imagines it as a trunk road connected to a number of roadsides, so I can access some of the points on the side of the road, and then return to the main road, where I have to use two times the number of steps to go through, because to return to the main road, and easy to find, This is the same for any side road.

Set D[i] means I distance from the root node, ans is the final answer, then Ans=max (Ans,d[i]+1+min (N-d[i]-1, (l-d[i))/2).

Sumoverpermutations

Test instructions

There is a wonderful, combinatorial mathematics is very slag, the teacher asked him: infinite n color of the ball placed in N order box, each box put one, the next box of the ball color, there are how many ways. This wonderful flower gave a wonderful solution, he said this and put the order, for example, there are three boxes, three colors of the ball, if placed in order is 1 2 3, then the answer is 3x2x2, if the order is 1 3 2, then the answer is 3x3x1. More generally, he thinks, if the left and right side of a position are put, then there is now (n-2) possibility, if only one side is released, then there are (n-1) possibility, if both sides are not put, then there are n possibilities. We know this is obviously wrong, but, the question is to ask you, give you a n, this n! kind of order according to this wonderful algorithm to get the answer is how much.

Exercises

Make $dp[i]$ said $n$ color in $i$ box, the answer is how much. Then there are only two cases of transfer:

One is to place the $i$ ball on the border, this transfer is $dp[i]=2*dp[i-1]* (n-1) $, the first item of 2 represents the left and right two boundaries, the second $dp[i-1]$ represents the case of $i-1$, the third $n-1$ that because the $i$ ball at the border, so only multiply $n -1$

The other is to put the $i$ ball in the middle of a position, assuming that the left side of the $j$ ball, then the transfer is necessarily

$ $DP [i]=\sum\limits_{j=1}^{i-2}c_{i-1}^j*dp[j]*dp[i-j-1]* (n-2) $$

So the total shift is

$ $DP [i]= (\sum\limits_{j=1}^{i-2}c_{i-1}^j*dp[j]*dp[i-j-1]* (n-2)) +2*dp[i-1]* (n-1) $$

The answer is obviously $dp[n]$.

Long LongDp[max_n];Long LongMod=1000000007;Long LongC[max_n][max_n];classsumoverpermutations{ Public:        intFindsum (intN) {c[0][0]=1;  for(intI=1; i<=n;i++)                 for(intj=0; j<=i;j++) C[i][j]= (j==0?1: c[i-1][j-1]+c[i-1][J])%MoD; dp[1]=n%MoD; dp[2]=n%mod* (n1)%mod*2%MoD;  for(intI=3; i<=n;i++) {Dp[i]=2%mod* (n1)%mod*dp[i-1]%MoD;  for(intj=1; j<=i-2; j + +) Dp[i]= (dp[i]+c[i-1][j]%mod*dp[j]%mod*dp[i-j-1]%mod* (n2)%mod)%MoD; }            returndp[n]%MoD; }};
View Code

Topcoder SRM 666 DIV 1

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.