Cattleya number of C ++

Cattleya number Introduction

Cattleya number known as Catalan number, Cattleya number is a mathematical combination is found in a variety of problems in counting the number of columns. Belgian mathematician Eugène Charles Catalan (1814-1894) named after. But first is the number of Catalan Euler solved the convex hull is divided into a triangle problem, when in 1753, launched.

Initial value: f (0) = f ( 1) = 1
Recurrence formula: f (n) = f ( 0) * f (n - 1) + f (1) * f (n - 2) + ...... + f (n - 1) * f (0)

solved problem:

  1. Parenthesized: P = a1 × a2 × a3 × ...... × an, based on the multiplication is associative, the order does not change, only the product of a pair of parentheses, brackets ask several of the program? (The answer: catalan [n])
  2. Stack order: a stack into the stack sequence 1,2,3, ..., n, the number of different sequences of the stack? (Answer: catalan [n])
  3. Convex polygon triangulation: in a convex polygon by diagonals disjoint several pieces, this polygon is divided into multiple triangles, how many different points system? (Answer: catalan [n - 2])
  4. The composition of a given node binary search tree: Given N nodes, how many different binary search tree can be constructed? (The answer: catalan [n])
  5. n to correctly match the number in parentheses: Given n parentheses, the number of strings correctly paired brackets is the number? (The answer: catalan [n])

Here I popped to the second question about the origin of the order of the number of Cattleya. First, we set f (n) represents the number sequence is a sequence number out of the stack of n species. (We assume that the last k elements of the stack, obviously, the case when different values ​​are independent of k, i.e. the number where k is obtained for each of the available final addition the pull principle, since k and finally the stack, Therefore, before the stack k, k smaller than the average value of the stack, there is the case where f (k-1) species, and after a value larger than k stack, the stack before and k are, therefore f (nk) manner, since the ratio k is smaller than k and a value larger push the stack case is independent, useful herein multiplication principle, co-f (nk) * f (k-1) kind, and k may take 1 ~ n, all of f (nk) * f (k-1) is the summation Catalan recursive formula.

Cattleya number of codes

With the recurrence relations, the code is simple to burst.

(PS: NR refers to the upper limit of the number of array elements catalan)

# include <cstdio>
# include <iostream>
# include <cmath>
# include <cstring>
# include <algorithm>
# include <climits>

using namespace std;

# define FOR(i, a, b) for(int i = a; i <= b; i++)
# define _FOR(i, a, b) for(int i = a; i >= b; i--)

const int NR = 100000;

int n;
long long catalan[NR + 10];

int main()
{
	scanf("%d", &n);
	catalan[0] = catalan[1] = 1;
	FOR(i, 2, n)
		FOR(j, 0, i - 1)
			catalan[i] += catalan[j] * catalan[i - j - 1];
	printf("%lld\n", catalan[n]);
	return 0;
}

God Bless You For Ever!

Published 33 original articles · won praise 47 · views 10000 +

Guess you like

Origin blog.csdn.net/SkeletonKing233/article/details/102649964