108 odd tricks for Linux programming-12 (storage and computing) continued

Source: Internet
Author: User

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

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.