HDU 1023 train Problem II large number of tables

Source: Internet
Author: User

How many sequence problems are there for an outbound stack. It is generally known that it is the catalan number.

The problem is that the number of Catalan is large, so high-precision computing is required.

In addition, table creation is much faster. Note the following formula:

Catalan number formula Cn = C (2n, N)/(n + 1 );
Recursive Formula C (n) = C (n-1) * (4 * N-2)/(n + 1)

The knowledge of high precision multiplied by an integer and high precision divided by an integer. In this way, it is better to use an integer array for computation. If you use a string, it is not easy to calculate it, because the integer may also be multiple digits.


Const int max_n = 101; short Catalan [max_n] [max_n]; // catlans [I] [0] Save the lengthvoid calcatalans () {short carry = 0, Len = 1; catalan [1] [0] = 1; Catalan [1] [1] = 1; Catalan [2] [0] = 1; Catalan [2] [1] = 2; for (INT I = 3; I <max_n; I ++) {carry = 0; For (Int J = 1; j <= Len; j ++) // multiply the precision by an integer {short sum = Catalan [I-1] [J] * (I <2)-2) + carry; carry = sum/10; catalan [I] [J] = sum % 10;} while (carry) {Catalan [I] [++ L En] = carry % 10; carry/= 10;} For (Int J = Len; j> 0; j --) // divide the precision by an integer {short sum = Catalan [I] [J] + carry * 10; Catalan [I] [J] = sum/(I + 1 ); carry = sum % (I + 1); // do not need to consider the remainder} while (Catalan [I] [Len] = 0) Len --; catalan [I] [0] = Len ;}} int main () {calcatalans (); int N; while (~ Scanf ("% d", & N) {for (INT I = Catalan [N] [0]; I> 0; I --) {printf ("% d ", catalan [N] [I]);} putchar ('\ n');} return 0 ;}



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.