The following problems belong to the Catalan sequence:
- Yes Individuals line up to enter the theater. Admission is $5. Among them only The individual has a 5 dollar bill and another People only have 10 yuan bills, and there are no other bills in the theater. How many ways are there so that as long as 10 yuan people buy tickets, there will be 5 yuan bills in change at the box office?
- A big-city lawyer lives just north of her home blocks and east of Work in blocks. Every day she goes blocks to work. If he never crosses (but can hit) the diagonal from home to office, how many possible paths are there?
- Select on circle points, connect these points in pairs so that the obtained How many ways can line segments not intersect?
- How many ways are there to divide a convex polygon area into triangular areas when the diagonals do not intersect?
- The push sequence of a stack (infinity) is How many different pop sequences are there?
- How many different binary trees can be constructed from each node?
- a and a constitute item , its parts and satisfies Correspondence What is the number?
The corresponding sequence is:
| ... | |||||||
|---|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 5 | 14 | 42 | 132 | ... |
(Catalan sequence)
Recursive
The solution to this recurrence relation is:
Common formulas for Catalan numbers:
The main idea of the question: The order of pushing onto the stack is $1,2,\ldots,n$, find the total number of all possible popping orders.
// C++ Version
#include <iostream>
using namespace std;
int n;
long long f[25]; int main() { f[0] = 1; cin >> n; for (int i = 1; i <= n; i++) f[i] = f[i - 1] * (4 * i - 2) / (i + 1); // Common formula 2 is used here cout << f[n] << endl; return 0;
}
path counting problem
A non-descending path is a path that can only go up or to the right.
from Arrive The number of non-descending paths is equal to a and a The number of permutations, that is 。
from Arrive does not touch the straight line except for the endpoints The number of non-descending paths:
Consider first The paths below all start from set off, pass and Arrive , can be seen as Arrive no contact The number of non-descending paths.
All non-descending paths have article. Have any contact with any of these? path, you can take its last point leaving this line to between the parts about Symmetric transformation, we get from Arrive a non-descending path. The reverse is also true. Thus The number of non-descending paths below is . According to the symmetry, the answer is 。
from Arrive does not pass through a straight line except for the endpoints The number of non-descending paths:
You can get it in a similar way: