UVa 10303 how Many trees? (Cattleya number & high precision)

Source: Internet
Author: User

10303-how Many trees?

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=1244

A binary search tree are a binary tree with root k such so any node v. in the left subtree of K has label (v) <label (k ) and any node W with the right subtree of K has label (W) > label (k).

When using binary search trees, one can easily look for a node with a given label x:after we-compare X to the label of th e root, either we found the node we seek or we know which subtree it are in. For most binary search trees the average time to find one of it n nodes in-way is O (log n).

Given a number n, can you tell how to many different binary search trees may is constructed with a set of numbers of size n s Uch that each element of the set is associated to the label of exactly one node in a binary search tree?

Input and Output

The input would contain a number 1 <= i <= 1000 per line representing the number of elements of the set. You are have to print a line in the output for each entry with the answer to the previous question.

Sample Input

1

2

3

Sample Output

1

2

5

One of the classic Cattleya. The recursive formula is shown in combinatorial mathematics.

Complete code:

Java: I have to say that in this topic finally reverse attack C + + ...

/*0.285s*/
    
import java.io.*;  
Import java.util.*;  
Import java.math.*;  
    
public class Main {public  
    static Scanner cin = new Scanner (new Bufferedinputstream (system.in));///nice speed public  
    
    s tatic void Main (string[] args) {  
        biginteger[] f = new biginteger[1001];  
        F[1] = Biginteger.one;  
        for (int i = 2; I <= 1000 ++i)  
            f[i] = f[i-1].multiply (biginteger.valueof (4 * i-2)). Divide (Biginteger.valueof ( i + 1));  
        while (Cin.hasnextint ())  
            System.out.println (F[cin.nextint ()));  
    }  

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

C++:

/*0.682s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;  
    
    const int MAXN = 700;///Note that this value is set to pay attention to intermediate result char numstr[maxn];///input Output interface struct Bign {int len, S[MAXN];  
        Bign () {memset (s, 0, sizeof (s));  
    len = 1;  
    } bign (int num) {*this = num;  
    } bign (const char* num) {*this = num;  
        } bign operator = (const int num) {char S[MAXN];  
        sprintf (S, "%d", num);  
        *this = s;  
    return *this;  
        } bign operator = (const char* num) {len = strlen (num);  
        for (int i = 0; i < len; i++) S[i] = num[len-i-1] & 15;  
    return *this;  ///Output Const char* str () const {if (len) {for (int i = 0; i < Len  i++) numstr[i] = ' 0 ' + s[len-i-1];
            Numstr[len] = ' the ';  
        else strcpy (Numstr, "0");  
    return numstr;  
    ///to the leading 0 void clean () {while (len > 1 &&!s[len-1]) len--;  
        ///plus bign operator + (const bign& b) const {bign C;  
        C.len = 0;  
            for (int i = 0, g = 0; G | | | i < MAX (Len, B.len); i++) {int x = g;  
            if (i < len) x + = S[i];  
            if (I < B.len) x + = B.s[i];  
            c.s[c.len++] = x% 10;  
        g = X/10;  
    return C;  
        ///minus bign operator-(const bign& B) const {bign C;  
        C.len = 0;  
            for (int i = 0, g = 0; i < len; i++) {int x = s[i]-G;  
            if (I < b.len) x-= B.s[i];  
            if (x >= 0) g = 0;  
                else {g = 1;  
x + 10;            } c.s[c.len++] = x;  
        } C.clean ();  
    return C;  
        ///Multiply bign operator * (const bign& b) const {bign C;  
        C.len = len + B.len; for (int i = 0; i < len; i++) for (int j = 0; J < B.len; J +) C.s[i + j] = = S[i] * b  
        . S[j];  
            for (int i = 0; i < c.len-1 i++) {c.s[i + 1] + = c.s[i]/10;  
        C.s[i]%= 10;  
        } C.clean ();  
    return C;  
        }///except bign operator/(const bign &b) const {bign ret, cur = 0;  
        Ret.len = Len;  
            for (int i = len-1 i >= 0; i--) {cur = cur * 10;  
            Cur.s[0] = s[i];  
                while (cur >= b) {cur = b;  
            ret.s[i]++;  
        } Ret.clean ();  
    return ret;  
    
   } Modulo, bign operator% (const bign &b) Const {bign c = *this/b;  
    return *this-c * b;  
        BOOL operator < (const bign& b) Const {if (len!= b.len) return Len < B.len;  
        for (int i = len-1 i >= 0; i--) if (S[i]!= b.s[i]) return s[i] < b.s[i];  
    return false;  
    BOOL operator > (const bign& B) Const {return B < *this; BOOL operator <= (const bign& B) Const {return! (  
    b < *this); BOOL operator >= (const bign &b) Const {return! (  
    *this < b); BOOL operator = = (CONST bign& b) Const {return! b < *this) &&!  
    (*this < b);  
    BOOL operator!= (const bign &a) const {return *this > A | | *this < A;  
   } bign operator = = (Const bign &a) {     *this = *this + A;  
    return *this;  
        } bign Operator-= (const bign &a) {*this = *this-a;  
    return *this;  
        } bign operator *= (const bign &a) {*this = *this * A;  
    return *this;  
        } bign operator/= (const bign &a) {*this = *this/a;  
    return *this;  
        } bign operator%= (const bign &a) {*this = *this% A;  
    return *this;  
    
}  
};  
    
Bign c[1001] = {1};  
    int main () {bign i;  
    int II;  
    for (i = 1, ii = 1; II <= 1000 i = i + 1, ++ii) c[ii] = c[ii-1] * ((i * 4)-2)/(i + 1);  
    while (~SCANF ("%d", &ii)) puts (C[ii].str ());  
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.