Number of Cattleya

Source: Internet
Author: User

Online search of the Great God's summary. Many pictures in the middle are omitted ... Later on.

Number of Catalan

English: Cattleya number

Principle:

Make the H (1) =1,h (0) =1,catalan number satisfy the recursive formula:

H (N) = h (1) *h (n-1) + H (2) *h (n-2) + ... + h (n-1) H (1) (where n>=2)

Alternative recursion Type:

H (N) = ((4*n-2)/(n+1)) *h (n-1);

The solution of this recursive relationship is:

H (n+1) =c (2n,n)/(n+1) (n=1,2,3,...)

I don't care how the solution is found, I just want to know how to use Catalan number to analyze the problem.

I summed up the most typical four types of applications: (essentially all the same, nothing more than the application of recursive equations, see if you can break down the problem to write a recursive type)

  1. the problem of parentheses.

Matrix chain multiplication: P=a1xa2xa3x......xan, according to the multiplication of the law, do not change its order, only in parentheses to denote the product of the pair, how do you have a number of bracketed schemes? (H (N) species)

  2. issue the stack order.

A stack (infinity) of the stack sequence is a three-in-one,.. N, how many different out-stack sequences are there?

Similar: There are 2n individuals lined up to enter the theater. Admission fee is 5 yuan. of which only n individuals have a 5-dollar bill, the other N people only 10 yuan banknotes, the theater no other banknotes, asked how many of the methods so long as there are 10 of people buy tickets, the ticket office has 5 yuan of money change? (will hold 5 yuan to see as the 5 yuan into the stack, holding 10 yuan to the view to make a stack of 5 yuan out of the stack)

More generally, when there are M individuals who have $5, n individuals have N $10, the total scheme number:

Number of legal sequences = total number of sequences-total of illegal sequences
The total number of sequences can be computed in such a m+n position, select n positions to fill 1, so is C (M+n, N)
The number of illegal sequences is: m+n position, select m+1 position to fill 1 so is C (M+n, m+1)
And then everyone is not the same, so you need to arrange the whole m! * n!

F (N) = (c (m+n,n)-C (m+n,m+1)) * m! * n!= (M+N)! * (m-n+1)/(m+1)

  3. Divide the multilateral lines into triangular issues.

How many ways to divide a convex polygon area into a triangular region?

Similarly: A barrister in a big city works N blocks north of her home and N blocks East. Every day she walks 2n blocks to work. If she

Never cross (but can touch) the diagonal from home to the office, so how many possible roads?

Similar: Select 2n points on a circle to connect these points in pairs so that the resulting n segments do not intersect the number of methods?

4. to the top node to form a two-fork tree problem.

Given n nodes, how many different two-fork trees can be formed?

(capable of constituting H (N))

The solution of Catalan number

The combination formula of Catalan number is cn=c (2n,n)/(n+1);

The recursive formula for this number is H (n) = h (n-1) * (4*n-2)/(n+1)

/* Large number solution

For large numbers, you should use the following large number algorithm.

The formula used is: H (n) = h (n-1) * (4*n-2)/(n+1);

*/

0ms

#include <iostream>

using namespace Std;

#define MAX 100

#define BASE 10000

void Multiply (int a[],int max,int b)//large number multiplication, note the transfer of parameters

{

int i,array=0;

for (i = Max-1; I >= 0;i--)

{

Array + = b *a[i];

A[i] = array%base; An array of four digits for each of the large number of bits

Array/=base;

}

}

void divide (int a[], int Max, int b)//Analog large number division

{

int i, div = 0;

for (i = 0; i < max;i++)

{

div = div *base + a[i];

A[i] = div/b;

Div%= B;

}

}

int main ()

{

int a[101][max],i, n;

memset (a[1],0,max*sizeof (int));

For (i=2, a[1][max-1] = 1; i < 101;i++)//high-coordinate storage for large number of low

{

memcpy (a[i],a[i-1], MAX * sizeof (int)); H[i] = h[i-1];

Multiply (A[i],max, 4 * i-2); H[i] *= (4*i-2);

Divide (A[i],max, i + 1); H[i]/= (i+1);

}

while (CIN >>n)

{

for (i = 0; i< MAX && a[n][i] = = 0; i++); Remove the number 0 before the array.

cout <<a[n][i++]; Output first non 0 number

for (; I <MAX; i++)

{

printf ("%04d", A[n][i]); Output after the number, and each bit is maintained 4 bit length! (32767)

}

cout <<endl;

}

return 0;

}

AC CODE 2:

C (0) = 1; (n+2) *c (n+1) = (4n+2) *c (n); That is: h (n) = h (n-1) * (4 * n-2)/(n+1);

0MS

#include <iostream>

using namespace Std;

int a[101][101] = {0};

int main ()

{

int n,i,j,len,r,temp,t;

int b[101];

A[1][0] = 1; Low-coordinate storage of large number of lows

len = 1;

B[1] = 1;

for (i = 2; I <=; i++)

{

t = i-1;

for (j=0;j<len;j++)//analog multiplication, starting from low

{

A[I][J] = a[i-1][j] * (4 * t + 2);

}

for (r = J =0; J < Len; j + +)//process Multiply results

{

temp = A[i][j] + R;

A[I][J] = temp% 10;

R = TEMP/10;

}

while (r)//Carry Processing

{

a[i][len++] = r% 10;

R/= 10;

}

For (j =len-1, r = 0; j >= 0; j--)//analog division, starting from high

{

Temp = R * + a[i][j];

A[I][J] = temp/(t+2);

r = temp% (t+2);

}

while (!a[i][len-1])//High 0 processing

{

len--;

}

B[i] = len; Record the length of the result

}

while (CIN >> N)

{

for (j = b[n]-1; J >= 0; j--)

{

printf ("%d", a[n][j]);

}

printf ("\ n");

}

return 0;

The "beauty of programming" mentions the question of "buy a ticket for change", and the following information, which is related to Cattleya number CN, is defined as follows:

The number of Cattleya is really a magical number, and many of the combinatorial problems are related to it, such as:

    • cn= the number of Dyck words with a length of 2n. Dyck words is a string of n x and N y, and the number of y from left to right is not more than X, for example, a length of 6 Dyck words:

xxxyyy xyxxyy xyxyxy Xxyyxy xxyxyy

    • cn= n the number of strings consisting of correctly matching parentheses, for example, 3 pairs of parentheses can be composed:

((())) ()(()) ()()() (())() (()())

    • cn= n+1 number multiplied by the number of parentheses. For example, a 4-digit parenthesized scheme is:

((AB) c) d (A (BC)) d (AB) (CD) A ((BC) d) A (b (CD))

    • Cn= has a number of two-fork trees with n+1 leaf nodes. For example, all two-forked tree patterns of 4 leaf nodes:

    • Cn=n*n's grid map, the number of paths from one corner to another, not across the diagonal, for example, the paths in a 4x4 grid map are:

    • cn= n+2 Polygon, the number of schemes that can be divided into triangles, such as the 6-sided split scheme:

    • Around the cn= round table there were 2n individuals, who shook hands 22, but did not cross the number of scenarios.

In the book "Enumerative Combinatorics", it was mentioned that up to 66 combinatorial problems were related to the number of Cattleya.

Analysis

"Cattleya number" in addition to the use of formulas can be calculated, you can also use "hierarchical arrangement" to solve. For example, in the case of a valid N-pair bracket, for a sequence ((), there are two left brackets, and a right parenthesis, which can be thought of as "offsetting a pair of parentheses and leaving a left bracket waiting to be offset", then it is also possible to add a right parenthesis at the end, or a left parenthesis, without the left parenthesis remaining,

As a result, the problem can be understood as a total of 2n brackets, the 1~2n level of the case, level I save all the remaining I left bracket arrangement of the number of schemes. The calculation process for the 1~8 level is the following table:

The calculation process is interpreted as follows: Level 1: Only 1 "("; 2 levels: One can be added at the end of the first level ")" or "("

After each level of calculation, if you encounter the remaining n>0 "(" scheme, you can add a "(" or ")" at the end to enter the next level; If you encounter the remaining n=0 "(" scheme, you can add a "(" to the next level at the end.)

An odd number of levels can only contain the remaining odd number of "(" arrangement schemes

An even number of levels can only contain the remaining even number of "(" arrangement schemes

As can be seen from the table, the gray part can be used without calculation.

Solution

The key code is:

Double Catalan (int n) {if (n = = 0) return 1, for (int i= 2; I <= 2 * n; i++) {var m = i <= n? i:2 * n + 1-i; for (int j = (i-1) & 1; J <= m; j + = 2) {if (J > 0) arr[j-1] + arr[j]; if (j< N) arr[j + 1] + = arr[j]; a RR[J] = 0; }} return arr[0]; }

which

n is the N in CN;

arr = new Double[n + 1];//arr[i] represents the time when there is a K-bracket, the remaining "(" the number of permutations of the number i is arr[1] = 1;

Discuss

The complexity of the algorithm is =o (n^2) and the space complexity is O (n+1). The advantage of this approach in terms of formula calculation is that there is no multiplication method, only addition.

Number of Cattleya

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.