Next article:108 odd tricks for Linux programming-12 (storage computing)
The ticket purchasing problem is actually a problem of combining mathematics. You can solve it directly.
Assume that the X axis is the person holding 50 yuan, and the Y axis is the person holding 100 yuan, then a correct solution is equivalent to a grid problem from (0, 0) to (n, n, each time, only one grid can be taken, either x plus 1, or y plus 1. A red line shown below is a solution for eight people, that is, {50, 50, 100,100, 50, 100,100 }, let's start with two 50-yuan buyers and two 100-yuan buyers.
The number of solutions from (0, 0) to (n, n) is defined by the grid problem. However, the solution that spans the diagonal line y = X must be deducted, this is equivalent to the number of solutions from (1,-1) to (n, n. Therefore, the result is the difference between two numbers. This value is the number of Catalan. Therefore, it is not necessary to directly calculate it from the method given above, in the actual calculation of the solution class, to prevent the overflow of large numbers, we can traverse the numbers of 2 and 5 in the product over and over again, and combine them into one pair, and add 0 at the end of the result, this avoids overflow to a certain extent.
Of course, this article mainly aims to introduce the storage and computing skills. I borrowed this example. In the previous article, we used the storage and computing method to enumerate every solution. Here is an introduction to storage computing.
Others in this seriesArticleRecommended: http://blog.csdn.net/pennyliang/category/746545.aspx