Hangzhou Electric Acm1130--how Many Tree? Number of ~~catalan

Source: Internet
Author: User

The recursive formula for the Catalan number is: f (n) = f (n-1) * (4*n-2)/(n + 1);

Since n is the maximum of 100, it is necessary to use an array to hold each of the Catalan numbers. You need to use a large number of operations.

The following is the code for the AC:

# include <stdio.h>struct Catalan{int length;int len[100];};              struct Catalan a[101];int main () {a[0].length = 1; a[0].len[0] = 1; First 4 Catalan number a[1].length = 1; A[1].len[0] = 1;a[2].length = 1; A[2].len[0] = 2;a[3].length = 1; A[3].len[0] = 5;int I, j, K, Num, N, Yu, div;for (i = 4; I <=; i++) {num = 4 * i-2; k = 0; yu = 0; div = 0;a[i].len Gth = 0;for (j = 0; J < A[i-1].length; J + +)//According to the formula, multiply by 4 * i-2, using a one-way method. {A[i].len[k] = (a[i-1].len[j] * num + yu)% 10;yu = (a[i-1].len[j] * num + yu)/10;k++;} while (Yu)//The remainder is not 0; continue to fill {a[i].len[k] = yu% 10;yu = yu/10;k++;} A[i].length = k;for (j = a[i].length-1; J >= 0; j--)//based on the formula, divided by i + 1, using a large number divided by a decimal. {div = div * + a[i].len[j];a[i].len[j] = div/(i + 1);d IV = div% (i + 1);} while (!a[i].len[a[i].length])//Remove the predecessor 0.a[i].length--;a[i].length++;} while (scanf ("%d", &n)! = EOF) {for (i = a[n].length-1; I >= 0; i--) printf ("%d", A[n].len[i]);p rintf ("\ n");}return 0;} 


Hangzhou Electric Acm1130--how Many Tree? Number of ~~catalan

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.