Hdu 2067 bunny Board (catlan number)

Source: Internet
Author: User

 

Rabbit Board

 

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission (s): 2323 Accepted Submission (s): 1360

Problem Description

The uncle of the rabbit came back from outside and brought her a gift. The rabbit happily ran back to his room and opened the board. The rabbit was disappointed. But a few days later I found the fun of the Board. The number of shortest paths from the start point (0, 0) to the end point (n, n) is C (2n, n ), now, the rabbit wants to determine the number of such paths without traversing the diagonal line (but can touch the grid points on the diagonal line? I have never thought about it for a long time. Now I want to ask you to help me solve this problem. It should be difficult for you!

 

Input

Input n (1 <= n <= 35) at a time, and end the input when n is equal to-1.

 

Output

For the number of output paths for each input data, see Sample for the specific format.

 

Sample Input

1

3

12

-1

 

Sample Output

1 1 2

2 3 10

3 12 416024

The most basic formula for finding other methods on the Internet is the catlan number (not a large number). I will summarize the catlan number first.

 

Code:

Java code

# Include <iostream>

# Include <stdio. h>

# Include <memory. h>

# Include <cmath>

Using namespace std;

 

Long h [45];

Long f [45] [45];

 

Void catalan2 () // catlan number: method 2

{

Int I, j;

For (I = 1; I <= 36; I ++)

{

F [0] [I] = 1;

}

For (I = 1; I <36; I ++)

{

For (j = I; j <= 36; j ++)

{

If (I = j)

{

F [I] [j] = f [I-1] [j];

}

Else

{

F [I] [j] = f [I-1] [j] + f [I] [J-1];

}

}

}

}

 

Void catalan () // catlan number: method 1

{

Int I, j;

H [0] = 1;

For (I = 1; I <36; I ++)

{

H [I] = 0;

For (j = 0; j <= I; j ++)

{

H [I] + = h [j] * h [i-j-1];

}

}

}

 

Int main ()

{

Int n, zz = 1;

Catalan ();

Catalan2 ();

While (scanf ("% d", & n )! = EOF)

{

If (n =-1) break;

// H [n] and f [n] [n] are all catlan numbers

// Printf ("% d % I64d \ n", zz ++, n, h [n] * 2 );

Printf ("% d % I64d \ n", zz ++, n, f [n] [n] * 2 );

}

 

Return 0;

}

Related Article

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.