UVA 10303 How Many Trees? (catlan)

刚开始没看出时卡特兰数列。直接套高精度版

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
const int numlen=1010;
struct bign {
    int len, s[numlen];
    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[numlen];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator = (const char *num) {
        len = strlen(num);
        while(len > 1 && num[0] == ‘0‘) num++, len--;
        for(int i = 0;i < len; i++) s[i] = num[len-i-1] - ‘0‘;
        return *this;
    }

    void deal() {
        while(len > 1 && !s[len-1]) len--;
    }

    bign operator + (const bign &a) const {
        bign ret;
        ret.len = 0;
        int top = max(len, a.len) , add = 0;
        for(int i = 0;add || i < top; i++) {
            int now = add;
            if(i < len) now += s[i];
            if(i < a.len)   now += a.s[i];
            ret.s[ret.len++] = now%10;
            add = now/10;
        }
        return ret;
    }
    bign operator - (const bign &a) const {
        bign ret;
        ret.len = 0;
        int cal = 0;
        for(int i = 0;i < len; i++) {
            int now = s[i] - cal;
            if(i < a.len)   now -= a.s[i];
            if(now >= 0)    cal = 0;
            else {
                cal = 1; now += 10;
            }
            ret.s[ret.len++] = now;
        }
        ret.deal();
        return ret;
    }
    bign operator * (const bign &a) const {
        bign ret;
        ret.len = len + a.len;
        for(int i = 0;i < len; i++) {
            for(int j = 0;j < a.len; j++)
                ret.s[i+j] += s[i]*a.s[j];
        }
        for(int i = 0;i < ret.len; i++) {
            ret.s[i+1] += ret.s[i]/10;
            ret.s[i] %= 10;
        }
        ret.deal();
        return ret;
    }

    bign operator * (const int num) {
//        printf("num = %d\n", num);
        bign ret;
        ret.len = 0;
        int bb = 0;
        for(int i = 0;i < len; i++) {
            int now = bb + s[i]*num;
            ret.s[ret.len++] = now%10;
            bb = now/10;
        }
        while(bb) {
            ret.s[ret.len++] = bb % 10;
            bb /= 10;
        }
        ret.deal();
        return ret;
    }

    bign operator / (const bign &a) 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 >= a) {
                cur -= a;
                ret.s[i]++;
            }
        }
        ret.deal();
        return ret;
    }

    bign operator % (const bign &a) const {
        bign b = *this / a;
        return *this - b*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; }

    bool operator < (const bign &a) const {
        if(len != a.len)    return len < a.len;
        for(int i = len-1;i >= 0; i--) if(s[i] != a.s[i])
            return s[i] < a.s[i];
        return false;
    }
    bool operator > (const bign &a) const  { return a < *this; }
    bool operator <= (const bign &a) const { return !(*this > a); }
    bool operator >= (const bign &a) const { return !(*this < a); }
    bool operator == (const bign &a) const { return !(*this > a || *this < a); }
    bool operator != (const bign &a) const { return *this > a || *this < a; }

    string str() const {
        string ret = "";
        for(int i = 0;i < len; i++) ret = char(s[i] + ‘0‘) + ret;
        return ret;
    }
};
istream& operator >> (istream &in, bign &x) {
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
ostream& operator << (ostream &out, const bign &x) {
    out << x.str();
    return out;
}
#define MAXN 1005
bign ans[MAXN];
int N;
void init()
{
    ans[1]=1;
    ans[2]=2;
    for (int i=3;i<MAXN;i++)
    {
        ans[i]=ans[i-1]*(4*i-2);
        ans[i]/=(i+1);
    }
}
int main()
{
    init();
    while (cin>>N)
    cout<<ans[N]<<endl;
    return 0;
}
时间: 2024-08-10 03:29:10

UVA 10303 How Many Trees? (catlan)的相关文章

UVa 10303 - How Many Trees?

题目:求n个节点(元素不同)的BST的个数. 分析:组合,计数,卡塔兰数,大整数.卡塔兰数的应用. 说明:利用递推计算卡塔兰数. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int C[1005][1005] = {0}; int main() { C[1][0] = 1; for (int i = 2 ; i < 1001 ; ++ i) { for

UVa 10562 Undraw the Trees 看图写树

转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将这些树转换成特定的括号表示的树 思路: 首先,观察样例,可以发现就是先序遍历的顺序,因此可以确定dfs 但是,还有几个地方需要考虑: 同一级的结点,在同一级的括号中 由于顺序满足先序遍历,因此不需要存储树,更不需要构建树,直接在遍历过程中输出即可. 空树:即输入为:# 时的树的处理,我不建议在此进行

UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的形状个数为:14 5个节点形成的二叉树的形状个数为:42 把n个节点对号入座有n!种情况 所以有n个节点的形成的二叉树的总数是:卡特兰数F[n]*n! 程序: 1 import java.math.BigInteger; 2 import java.util.Scanner; 3 public cl

uva 10562 undraw the trees(烂题) ——yhx

无需建树,递归即可. 为什么说是烂题呢? 1.pdf里的样例数据复制过来丢了空格,导致我调了很久都没有发现问题在哪. 2.如样例2所示,4个'-'下面却少一格. 3.空树,即只有'#'需要特殊处理. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 char map[210][210]; 7 int m

UVa 10007 - Count the Trees

题目:统计n个节点的二叉树的个数. 分析:组合,计数,卡特兰数,大整数. n个节点的二叉树的形状有Cn个,求不同的树的个数,用卡特兰数乘以全排列n! 说明:打表计算,查询输出,提高效率. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int C[305][2005] = {0}; int main() { C[1][0] = 1; for (int i = 2

UVa 10562 Undraw the Trees

题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int maxn=210;int n;char a[maxn][ma

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

二叉树基础练习

前序遍历(先根遍历):根,左子树,右子树 中序遍历:左子树,根,右子树后序遍历:左子树,右子树,根 先序遍历:ABDECF 中序遍历:DBEAFC 后序遍历:DEBFCA 层次遍历:ABCDEF UVA 112  Tree Summing 题目:给你一个数和一棵树,问是否存在根到叶子的路径使得路径上的数字和与已知数相等. 注意数据中可能有负数 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #i

UVA - 10250 - The Other Two Trees (简单计算几何)

UVA - 10250 The Other Two Trees Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem E The Other Two Trees Input: standard input Output: standard output Time Limit: 2 seconds You have a quadrilateral