HDU 1023 Train Problem II 大数catalan

#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 10;

/// h(n) = (4 * n - 2)/(n + 1)*h(n - 1);

int catalan[N][N];

void init()
{
    catalan[1][0] = 1;
    for(int i=2; i<101; i++)
    {
        int tmp = 0;
        for(int j=0; j<N; j++)
        {
            tmp += catalan[i-1][j]*(4 * i - 2);
            catalan[i][j] = tmp % 10;
            tmp /= 10;
        }
        int j;
        for(j = N - 1; j >= 0; j --)
            if(catalan[i][j]!=0)
            break;
        for(; j >= 0; j--)
        {
            tmp = tmp * 10 + catalan[i][j];
            if(tmp >= i+1)
            {
                catalan[i][j] = tmp / (i+1);
                tmp %= (i + 1);
            }
            else
                catalan[i][j] = 0;
        }
    }
}

int main()
{
    init();
    int n;
    while(~scanf("%d", &n))
    {
        int i=N-1;
        for(; i>=0; i--)
            if(catalan[n][i]!=0)
                break;
        for(; i>=0; i--)
            printf("%d", catalan[n][i]);
        printf("\n");
    }
    return 0;
}

刚开始一直不想写,感觉不好写,还是有点怵,认真去写的话还是能写出来的

posted @ 2018-09-07 20:03  _簡單é  阅读(89)  评论(0编辑  收藏  举报