Cattleya number (Catalan) formula, demonstrated codes, typical example.

Section of this article switched https://www.cnblogs.com/yuzilan/p/10626072.html, the large number of cattle for analysis Cartland can be said to be a very, very detailed! Thanks for sharing predecessors!

1. Definitions

Cattleya number (Catalan number), the English name Catalan number, is a combination of mathematics often appear in various counting problems that appear in sequence.
It is the first few (starting with the first zero entries):

C0 = 1,         
C1 = 1,         C2 = 2,          C3 = 5,          C4 = 14,          C5 = 42,
C6 = 132,       C7 = 429,        C8 = 1430,       C9 = 4862,        C10 = 16796,
C11 = 58786,    C12 = 208012,    C13 = 742900,    C14 = 2674440,    C15 = 9694845,
C16 = 35357670, C17 = 129644790, C18 = 477638700, C19 = 1767263190, C20 = 6564120420, ...

2. Official

Here are just a simple and common

Term formula: Cn 11 + n (2nn) = 11 + nCn2n = (2n) = (n + 1) n Cn = 1 + n1 (n2n) = 1 + n1 C2nn = (n!!! +1)! n! (2n)!

3. Catalan derivation formula

Formula Deduction of interest, then there may be a small partner Baidu (I really could not stand ...... o (╥﹏╥) o to not stick out)

4. The number of code implements Cattleya

//函数功能: 计算Catalan的第n项
//函数参数: n为项数
//返回值:  第n个Catalan数
int Catalan(int n)
{
	if(n<=1) return 1;
	int *h = new int [n+1]; //保存临时结果
	h[0] = h[1] = 1;        //h(0)和h(1)
	for(int i=2;i<=n;++i)    //依次计算h(2),h(3)...h(n)
	{
		h[i] = 0;
		for(int j = 0; j < i; j++) //根据递归式计算 h(i)= h(0)*h(i-1)+h(1)*h(i-2) + ... + h(i-1)h(0)
			h[i] += (h[j] * h[i-1-j]);
	}
	int result = h[n]; //保存结果
	delete [] h;       //注意释放空间
	return result;
}

5. Code Example

1. Stack the order of
a stack (infinity) into the stack of sequence 1,2,3, ..., n, the number of different pop sequence?

  1. First, we set f (n) = number of sequence types Stack sequence number of n.

  2. It is also assumed from the beginning to the stack until the first space, the first ordinal number of the stack of this process is k.
    In particular, if the stack is empty until the end of the process, the k = n.

  3. A first stack of the blank prior to the first sequence number k to 1 ~ n of the sequence into two sequences: wherein a is 1 ~ k-1, the sequence number k-1; the other is a k + 1 ~ n, the sequence The number is nk.

  4. In this case, if we considered the k determines a sequence number, then in accordance with the principle of multiplication, F (n) is equivalent to the problem - the number k-1 is the sequence number of sequences out of the stack thereof multiplied by the number of sequences is n - stack of several sequences of k (a recursive thought), i.e., the number k is chosen sequence f (n) = f (k-1) × f (nk).

  5. And k may be selected from 1 to n, then it is the principle of addition, the number of different types of sequences k takes values ​​are added, the total number of sequences obtained as species: f (n) = f (0) f (n-1) + f (1) f (n-2) + ...... + f (n-1) f (0) f (n) = f (0) f (n-1) + f (1) f (n-2) + ...... + f (n-1) f (0)

  6. This formula and the number of recursive Cattleya same, namely f (n) = h (n) = 1n + 1Cn2n = Cn2n-Cn + 12nf (n) = h (n) = n + 11 C2nn = C2nn -C2nn + 1. Finally, let f (0) = 1, f (1) = 1.

    Solutions which is equal to the number of the n-th Catalan.


2.01 sequence
gives a n, requires a 2n sequence of length 01, so that any prefix sequence number not less than 1 number of zeros, the number of different sequences of 01?
Or less with a length of 6 sequence:
111000 101 100 101 010 110 010 110 100

With the above order of the stack analysis of that question, this question seems familiar, its essence is the order of the stack variants:

  1. Because for every number, it must be a push, pop once. We set the stack into the state '1', the state of the stack is set to '0'. All states corresponding to the number n and n-1 of n 0 2n-bit binary number thereof.
  2. Since the number of stack operation waits in order of arrangement 1 ‥ n, b push the operand stack is greater than or equal to the operand a (a≤b), the total number of the output sequence is scanned from left to right by n = 0 n-1 and 2N-bit binary number, the cumulative number is not less than 1 0 number of several integrated program.

Operation of the problem:

  1. Fill number n in Scheme 1 for 2n-bit binary number Cn2nC2nn, do not fill the remaining n bits 0 1 self-packing.
  2. Does not meet the requirements subtracted (left to right scan, the cumulative number is greater than the accumulated number of 0 to 1) the number of programs is also desired.
  3. Undesirable feature is that the number of scanning from left to right, bound to m + 1 and an accumulated number of 0's cumulative number of m 1 first appears on a bit odd bit 2m + 1, 2n-2m- thereafter nm has a '1' nm-1 and a '0' bit on 1
  4. Should on the rear 0 2n-2m-1 and 1 Bits These interchangeable, making the nm '0' nm-1 and a '1', the result obtained by the 1 of n + 1 '0' and n- a '1' 2N digits, i.e. a non-conforming to the array corresponds to the number of n + 1 of a '0' and the n-1 '1' thereof.
  5. Conversely, 2n-bit binary number consisting of any of n + 1 '0' and the n-1 '1' composition, since the number of the plurality 2 0, 2n is an even number, it will appear at a certain odd bit 0 cumulative number exceeds the cumulative number 1. Also in the rear portion 1 and 0 interchanged, making 2n by n bits '0' and n '1' composition, i.e., the n + 1 '0' and the n-1 '1' consisting of 2n digit number will correspond to a non-compliant.
  6. Thus undesired 2n, n + 1 bits and '0', n-1 '1' arranged in correspondence thereof.
  7. Obviously, it does not meet the requirements of the program number Cn + 12nC2nn + 1. This results in the total number of the output sequence f (n) = h (n ) = Cn2n-Cn + 12n = 1n + 1Cn2nf (n) = h (n) = C2nn -C2nn + 1 = n + 11 C2nn .
    Solutions which is equal to the number of the n-th Catalan .

3. '+1' 1 'sequence

n and n + 1 th number of 2n items constituting a1 -1, a2, ⋅⋅⋅, a2na1, a2, ⋅⋅⋅, a2n, and some of its properties satisfy the non-negative, i.e., a1 + a2 + ⋅⋅⋅ + ak> = 0a1 + a2 + ⋅⋅⋅ + ak> = 0, (k = 1,2, ···, 2n), this sequence number has a different?

Example 01 Analysis and Code This sequence analysis identical, i.e. equal to the number n th Catalan number of this series, it is not repeated here.
Solutions which is equal to the number of the n-th Catalan.


4. The sequence in brackets

n How many matching pair of parentheses way?
Sequence 3, for example, the legitimate bracket pair of brackets are configured C3 = 5C3 = 5 ratings: ((())), () (()), () () (), (()) (), (() ( )).

Its essence is the "01 series" Variant :
We left parenthesis to state '1', right parenthesis to state '0'. n brackets for all the n state '1' and the n '0' 2n-bit binary number corresponds.
Because there must be left parenthesis, right parenthesis to match, the output of a 2n-bit binary number in brackets the total number of correct sequences scanned from left to right by n = '1' and the n '0' composed of a number of non-cumulative several kinds of programs accumulated number less than zero.

Solutions which is equal to the number of the n-th Catalan.

5. Keep the change problem

2n personal five dollars to buy a ticket for the movie tickets, buy only one per person, but the conductor does not give change money. Where, n individual holding five dollars, held by individuals in addition n ten dollars and asked difficult in the case of the change does not occur, how many queuing?

Its essence is the "01 series" of variants:
We who hold five dollars to the state '1', who holds ten dollars to the state '0'. All states corresponding to the individual n-2n and n number '0' 2n-bit binary number '1'.
Because you must have a person holding five dollars came in front of a man holding ten dollars, give change can not happen difficult, so the total number of difficult change for queuing method of scanning does not occur by the left to right = n '1' and the n '0' of the 2N bit binary number, a total number of not less than several programs accumulated number of 0's.

Solutions which is equal to the number of the n-th Catalan.

6. Matrix Chain

P = a1 × a2 × a3 × ...... × an, based on the multiplication is associative, the order does not change, only a pair of parentheses represents the product, how several brackets of the program?

:( idea is consistent with the "Stack order" thinking )

  1. It can be considered by first parenthesized, the P-divided into two parts, and then each of the two parts of the bracket. (Recursive divide and conquer idea)

  2. For example into (a1) × (a2 × a3 ... × an), and then on (a1) and (a2 × a3 ... × an) of the respective brackets; and if divided into (a1 × a2) × (a3 ... × an), then again (a1 × a2) and (a3 ... × an) parenthesized.

  3. Several Let n matrix brackets scheme for f (n), then the solution of the problem is
    f (n) = f (1 ) * f (n-1) + f (2) * f (n-2) + f (3) * f ( n-3) + f (n-1) * f (1). f (1) * f (n -1) represents into (a1) × (a2 × a3 ... × an) in two parts, and each of the brackets.

Several calculation-start, f (1) = 1, f (2) = 1, f (3) = 2, f (4) = 5. Binding recursive, find that f (n) is equal to h (n-1).

Its solution equal to the number n-1 th Catalan.

7. binary count

There are n nodes consisting of binary (non-leaf node has two sons), there are many kinds of situations?
There are number of n + 1 binary tree leaves?

These two issues are really the same problem.
Examples: n = 3 in case of:

Here Insert Picture Description
Ideas :( recursive divide and conquer idea )

  1. It can be considered, the root must occupy a node, then the remaining n-1 nodes can have the following distribution, T (0, n-1), T (1, n-2), ..., T ( n-1,0) T (0, n-1), T (1, n-2), ..., T (n-1,0). Set T (i, j) T (i, j) represents a left subtree containing the root nodes i, j right subtree containing nodes.
  2. For the case then the root T (i, j) T (i, j) and then the left subtree has the following distribution: T (0, i-1), T (1, i-2), ..., T (i-1,0) T (0, i-1), T (1, i-2), ..., T (i-1,0), its right subtree allocation: T (0, j-1 ), T (1, j-2), ..., T (j-1,0) T (0, j-1), T (1, j-2), ..., T (j-1,0)
  3. Resetting solution of f (n), then f (n) = f (0) * f (n-1) + f (1) * f (n-2) + ... + f (n-2) * f (1) + f (n-1) * f (0). Suppose f (0) = 1, then f (1) = 1, f (2) = 2, f (3) = 5. Binding recursive, find that f (n) is equal to h (n).

Solutions which is equal to the number of the n-th Catalan.


8. convex polygon division

In an n-sided polygon by polygon does not intersect the n internal diagonal, the n-gon is split into a plurality of triangles, and asked how many programs split?

The pentagon has the following five kinds of splitting schemes:
Here Insert Picture Description
The following 14 kinds of hexagonal splitting schemes:

Here Insert Picture Description
Ideas :( recursive divide and conquer idea )

  1. A convex side of the polygon as a base, this edge is provided two vertices A and B. Selected from the remaining one vertex of the convex polygon may be divided into three parts, the middle of a triangle, two left and right sides are convex polygon, then solving for about two convex polygons.
  2. Problem solutions provided F (n), where n denotes the number of vertices, then f (n) = f (2 ) * f (n-1) + f (3) * f (n-2) + ...... + f ( n-2) * f (3 ) + f (n-1) * f (2).
    Wherein, f (2) * f ( n-1) represented by: three neighboring vertices of a triangle, the number of vertices of the other two portions are 2 (two points in a straight line), and n-1.
    Wherein, f (3) * f ( n-2) represented by: The convex polygon is divided into three parts, respectively a right and left vertexes of the triangle 3 and n-2 have a polygon vertices.
  3. Provided f (2) = 1, then f (3) = 1, f (4) = 2, f (5) = 5. Binding recursive, find that f (n) is equal to h (n-2).

Solutions which is equal to n-2-th Catalan number.


9. n on a circle line segments

2n selected points on the circle, the points are connected together in pairs such that the number n of line segments obtained disjoint method?

Ideas :( recursive divide and conquer idea )

  1. Wherein at a base point, numbered '0', then the other point in a clockwise direction are sequentially numbered.
  2. Then the number is '0' connected to the numbered points must be odd (otherwise, contain an odd number between the two points, points are bound to be isolated). I.e., the number is '0' at any point and a point of odd numbered linked to form a segment.
  3. The selected set '0' base point A, point B is connected to it, then all the points A and B is divided into two parts, part A is located, the left side of B, and the other portion is located, to the right of the A B. Then each of the two parts can be solved.
  4. Resetting solution f (n), then f (n) = f (0 ) * f (n-1) + f (1) * f (n-2) + f (2) * f (n-3) + ... + f (n-2 ) * f (1) + f (n-1) * f (0).
    Wherein, f (0) * f ( n-1) represents the number 0 is connected to the point and the point number 1, the number thereof is located right at this time point is zero (0 can be connected into line segments), are located on the left the point is 2n-2 (n-1 can be connected to a line segment). And so on.
  5. So that f (0) = 1, f (1) = 1, f (2) = 2.
  6. Binding recursive, find that f (n) is equal to h (n).

Solutions which is equal to the number of the n-th Catalan.


10. monotonic path

A big city lawyer in n n blocks east and one block north of his home, he walked every day 2n blocks to work. If he never crossing (but can be touch) diagonal from home to the office, then how many possible way?

analysis:

  1. A monotone path from the lower left corner of the grid points, ending at the top right of the grid points, each step are up or to the right. X stands for "right", Y stands for "up."
  2. Prefix can be found in any number of steps the number of 'X' is not less than the number of 'Y' is.
  3. The question then is converted into "01 series" Problems variant "sequence XY" Problem: Given a n, requires a sequence of length 2n XY, so that any prefix sequence X and the number Y is not less than the number how many different sequences XY?

Examples: n = 4 in the case of:
Here Insert Picture Description

Solutions which is equal to the number of the n-th Catalan.

11. fill staircase pattern

A height of the stepped pattern of n, the number of rectangles filled with n?

Examples: n = 4 in the case of
Here Insert Picture Description
thinking :( recursive partition Thought )
1. The height of the stepped pattern of the n-1, n of the height into the stepped pattern, the height of the stepped pattern is divided into n number of section.

1. The solution of the problem set f (n), where n represents the height of the stepped pattern of n or n rectangles.

2. First look n = 1, easy to get f (1) = 1; Similarly f (2) = 2. Where f (1) not only represents the height of a stepped FIG. 1, but showing a length or width there is 1 and n is not equal to one another rectangle.

3.那n=3呢?其实,n=3即在n=2的阶梯图形上再添加一个高度为1宽度为3(或高度为3宽度为1)的长方形,而且只有两种添加方法,即要么在左边添加,要么在上边添加,这样才能构成一个高度为n=3的阶梯状图形。

4.我们设f(0)=1。f(0)代表:一边为n,一边为1的长方形。

5.那么f(3)=f(0)*f(2)+f(1)*f(1)+f(2)*f(0)=5。
其中,f(0)*f(2)表示:高度为3的阶梯状图形含有这两个部分,一个部分是高度为2的阶梯状图形,另外一个部分是一边为3一边为1的长方形。
其中,f(1)*f(1)表示:高度为3的阶梯状图形含有这两个部分,都是高度为1的阶梯状图形。

6.那么f(4)=f(0)*f(3)+f(1)*f(2)+f(2)*f(1)+f(3)*f(0)=14。
其中,f(0)*f(3)表示:高度为4的阶梯状图形含有这两个部分,一个部分是高度为3的阶梯状图形,另外一个部分是一边为4一边为1的长方形。
其中,f(1)*f(2)表示:高度为4的阶梯状图形含有这两个部分,一个部分是高度为1的阶梯状图形或者长或宽有一条为1且另一条不等于n的长方形,另外一个部分是高度为2的阶梯状图形。

7.结合递推式,不难发现f(n) 等于h(n)。

其解等于第n个Catalan数。

12. 摞碗问题

饭后,姐姐洗碗,妹妹把姐姐洗过的碗一个一个放进碗橱摞成一摞。一共有n个不同的碗,洗前也是摞成一摞的,也许因为小妹贪玩而使碗拿进碗橱不及时,姐姐则把洗过的碗摞在旁边,问:小妹摞起的碗有多少种可能的方式?

此典例解析是“出栈问题”的变式,其解析一模一样,即此数列的个数等于第n个Catalan数,此处就不再赘述。

其解等于第n个Catalan数。

13. 汽车胡同加油问题

一个汽车队在狭窄的路面上行驶,不得超车,但可以进入一个死胡同去加油,然后再插队行驶,共有n辆汽车,问共有多少种不同的方式使得车队开出城去?

This resolution is typical example " popping problem " Variant, which parses the same, i.e., this number is equal to the number of columns of the n-th number of Catalan, will not repeat herein.

Solutions which is equal to the number of the n-th Catalan.

14. The question also book library

In the library a total of 2n people in the queue, n is also a "interview Collection," a book, n in a borrowed "interview Collection," a book, library book at this time there is no interview, and ask them to line up the total?

Analysis:
people still book library is always greater than or equal person, i.e. Cn2n-Cn-12nC2nn -C2nn-1
This is a typical example of parsing a "homing problem" variants, i.e., this number is equal to the number of columns n number of Catalan.

Solutions which is equal to the number of the n-th Catalan.

15. The height queuing problem

2n a height different people, arranged in two rows, each row must be arranged from low to high, but high in the first row of the second person parallelism corresponding to the arrangement and asked how many?

Solutions which is equal to the number of the n-th Catalan.

Guess you like

Origin blog.csdn.net/weixin_40688217/article/details/94750175