zoukankan      html  css  js  c++  java
  • hdu 1023 Train Problem II

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1212

    Train Problem II

    Description

    As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.

    Input

    The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.

    Output

    For each test case, you should output how many ways that all the trains can get out of the railway.

    SampleInput

    1
    2
    3
    10

    SampleOutput

    1
    2
    5
    16796

    卡特兰数。。

    $ f(n) = f(n-1)*(n*4-2)/(n-1)$

      1 #include<algorithm>
      2 #include<iostream>
      3 #include<cstdlib>
      4 #include<cstring>
      5 #include<cassert>
      6 #include<cstdio>
      7 #include<vector>
      8 #include<string>
      9 #include<map>
     10 #include<set>
     11 using std::cin;
     12 using std::max;
     13 using std::cout;
     14 using std::endl;
     15 using std::string;
     16 using std::istream;
     17 using std::ostream;
     18 #define sz(c) (int)(c).size()
     19 #define all(c) (c).begin(), (c).end()
     20 #define iter(c) decltype((c).begin())
     21 #define cls(arr,val) memset(arr,val,sizeof(arr))
     22 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
     23 #define rep(i, j, n) for (int i = j; i < (int)(n); i++)
     24 #define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++)
     25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
     26 #define pb(e) push_back(e)
     27 #define mp(a, b) make_pair(a, b)
     28 struct BigN {
     29     typedef unsigned long long ull;
     30     static const int Max_N = 2010;
     31     int len, data[Max_N];
     32     BigN() { memset(data, 0, sizeof(data)), len = 0; }
     33     BigN(const int num) {
     34         memset(data, 0, sizeof(data));
     35         *this = num;
     36     }
     37     BigN(const char *num) {
     38         memset(data, 0, sizeof(data));
     39         *this = num;
     40     }
     41     void clear() { len = 0, memset(data, 0, sizeof(data)); }
     42     BigN& clean(){ while (len > 1 && !data[len - 1]) len--;  return *this; }
     43     string str() const {
     44         string res = "";
     45         for (int i = len - 1; ~i; i--) res += (char)(data[i] + '0');
     46         if (res == "") res = "0";
     47         res.reserve();
     48         return res;
     49     }
     50     BigN operator = (const int num) {
     51         int j = 0, i = num;
     52         do data[j++] = i % 10; while (i /= 10);
     53         len = j;
     54         return *this;
     55     }
     56     BigN operator = (const char *num) {
     57         len = strlen(num);
     58         for (int i = 0; i < len; i++) data[i] = num[len - i - 1] - '0';
     59         return *this;
     60     }
     61     BigN operator + (const BigN &x) const {
     62         BigN res;
     63         int n = max(len, x.len) + 1;
     64         for (int i = 0, g = 0; i < n; i++) {
     65             int c = data[i] + x.data[i] + g;
     66             res.data[res.len++] = c % 10;
     67             g = c / 10;
     68         }
     69         return res.clean();
     70     }
     71     BigN operator * (const BigN &x) const {
     72         BigN res;
     73         int n = x.len;
     74         res.len = n + len;
     75         for (int i = 0; i < len; i++) {
     76             for (int j = 0, g = 0; j < n; j++) {
     77                 res.data[i + j] += data[i] * x.data[j];
     78             }
     79         }
     80         for (int i = 0; i < res.len - 1; i++) {
     81             res.data[i + 1] += res.data[i] / 10;
     82             res.data[i] %= 10;
     83         }
     84         return res.clean();
     85     }
     86     BigN operator * (const int num) const {
     87         BigN res;
     88         res.len = len + 1;
     89         for (int i = 0, g = 0; i < len; i++) res.data[i] *= num;
     90         for (int i = 0; i < res.len - 1; i++) {
     91             res.data[i + 1] += res.data[i] / 10;
     92             res.data[i] %= 10;
     93         }
     94         return res.clean();
     95     }
     96     BigN operator - (const BigN &x) const {
     97         assert(x <= *this);
     98         BigN res;
     99         for (int i = 0, g = 0; i < len; i++) {
    100             int c = data[i] - g;
    101             if (i < x.len) c -= x.data[i];
    102             if (c >= 0) g = 0;
    103             else g = 1, c += 10;
    104             res.data[res.len++] = c;
    105         }
    106         return res.clean();
    107     }
    108     BigN operator / (const BigN &x) const {
    109         BigN res, f = 0;
    110         for (int i = len - 1; ~i; i--) {
    111             f *= 10;
    112             f.data[0] = data[i];
    113             while (f >= x) {
    114                 f -= x;
    115                 res.data[i]++;
    116             }
    117         }
    118         res.len = len;
    119         return res.clean();
    120     }
    121     BigN operator % (const BigN &x) {
    122         BigN res = *this / x;
    123         res = *this - res * x;
    124         return res;
    125     }
    126     BigN operator += (const BigN &x) { return *this = *this + x; }
    127     BigN operator *= (const BigN &x) { return *this = *this * x; }
    128     BigN operator -= (const BigN &x) { return *this = *this - x; }
    129     BigN operator /= (const BigN &x) { return *this = *this / x; }
    130     BigN operator %= (const BigN &x) { return *this = *this % x; }
    131     bool operator <  (const BigN &x) const {
    132         if (len != x.len) return len < x.len;
    133         for (int i = len - 1; ~i; i--) {
    134             if (data[i] != x.data[i]) return data[i] < x.data[i];
    135         }
    136         return false;
    137     }
    138     bool operator >(const BigN &x) const { return x < *this; }
    139     bool operator<=(const BigN &x) const { return !(x < *this); }
    140     bool operator>=(const BigN &x) const { return !(*this < x); }
    141     bool operator!=(const BigN &x) const { return x < *this || *this < x; }
    142     bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); }
    143     friend istream& operator >> (istream &in, BigN &x) {
    144         string src;
    145         in >> src;
    146         x = src.c_str();
    147         return in;
    148     }
    149     friend ostream& operator << (ostream &out, const BigN &x) {
    150         out << x.str();
    151         return out;
    152     }
    153 }A[101], B;
    154 inline void init() {
    155     A[1] = 1;
    156     rep(i, 2, 101) {
    157         B = 4 * i - 2;
    158         A[i] = A[i - 1] * B / (i + 1);
    159         B.clear();
    160     }
    161 }
    162 int main() {
    163 #ifdef LOCAL
    164     freopen("in.txt", "r", stdin);
    165     freopen("out.txt", "w+", stdout);
    166 #endif
    167     init();
    168     std::ios::sync_with_stdio(false);
    169     int n;
    170     while (cin >> n) cout << A[n] << endl;
    171     return 0;
    172 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    c8051f交叉开关
    8052定时器2的用法
    poj1010
    poj2101
    poj1958
    poj3444
    poj2977
    DataTable 相关操作
    C#中string和StringBuilder的区别
    DataTable排序,检索,合并,筛选
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4578972.html
Copyright © 2011-2022 走看看