Hdu4489 the king's ups and downs

Source: Internet
Author: User
The King's ups and downs

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 163 accepted submission (s): 101

Problem descriptionthe king has guards of all different heights. rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next
He (so the heights go up and down along the Line). For example, seven guards of heights 160,162,164,166,168,170 and 172. cocould be arranged:

Or perhaps:

The King wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. to be able to do this, he needs to know for a given number of guards, N, how many different up and down orders
There are:

For example, if there are four guards: 1, 2, 3, 4 can be arrange:

1324,214 3, 3142,231 4, 3412,423 1, 4132,241 3, 3241,142 3

For this problem, you will write a program that takes as input a positive integer N, the number of guards and returns the number of up and down orders for N guards of differing heights.

Inputthe first line of input contains a single integer p, (1 <= P <= 1000), which is the number of data sets that follow. each data set consists of single line of input containing two integers. the first integer, D is the data set number.
The second integer, n (1 <= n <= 20), is the number of guards of differing heights.

Outputfor each data set there is one line of output. It contains the data set number (d) followed by a single space, followed by the number of up and down orders for the n guards.

Sample Input

 
41 12 33 44 20

Sample output

 
1 12 43 104 740742376475050

Sourcegreater New York 2012

Recommend: Alas, I don't want to say that my DP has taken a long time, and I didn't introduce a recursive formula. There are several errors in the tangle of small and small sizes. In fact, several troubleshooting issues are not final, because the size is relative ,....... We use answer [I] [0] to indicate a low end, and answer [I] [1] to indicate a high end. How can we find the final result? We thought that we must add the highest value at the end, so it must end with a low end, and the right side must start with a low end. Then the total number of I methods is, sum {DP [J] [0] * DP [n-j-1] * C [I, j]}; how does this formula come from? You think, we select J people in the I-1, these people must be high and low, then certainly, can form a low end and the number of the beginning with a high, this is actually obtained in the previous step. Do the two want to multiply the total number? Because what ends at a low end and ends at a high end is a change in order, which must be equal, in this way, you can update the question. This question is about to begin with! Answer [I] [0], and answer [I] [1];

# Include <stdio. h >#include <iostream> using namespace STD ;__ int64 answer [30] [2] ;__ int64 POW (int n) {If (n = 0) return 1; _ int64 sum = 1, I; while (n) {sum * = N; n --;} return sum ;}__ int64 FC (INT N, int m) {return POW (N)/POW (m)/POW (n-m);} int main () {int N, I, j, tcase, T; scanf ("% d", & tcase); While (tcase --) {scanf ("% d", & T, & N); If (n = 1) {printf ("% d 1 \ n", T); continue;} answer [0] [0] = 1; answer [0] [1] = 1; answer [1] [0] = 1; answer [1] [1] = 1; answer [2] [1] = 1; answer [2] [0] = 1; for (I = 3; I <= N; I ++) {_ int64 temp = 0; For (j = 0; j <I; j ++) {temp + = answer [J] [0] * answer [i-j-1] [1] * fc (I-1, J );} answer [I] [0] = answer [I] [1] = temp/2; // printf ("% d % i64d % i64d \ n", I, answer [I] [0], answer [I] [1]);} printf ("% d % i64d \ n", T, answer [N] [0] * 2);} return 0 ;}

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.