码迷,mamicode.com
首页 > 其他好文 > 详细

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

时间:2017-05-03 11:30:12      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:php   print   cat   names   pac   category   max   item   题目   

题目链接:uva 1478 - Delta Wave

题目大意:对于每一个位置来说,能够向上,水平,向下。坐标不能位负。每次上下移动最多为1。 给定n问说有多少种不同的图。结果对10100取模。

解题思路:由于最后都要落回y=0的位置,所以上升的次数和下降的次数是同样的,而且上升下降的关系满足出栈入栈的关系。即卡特兰数。


所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和。

C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)i?(i+1)
注意取模后的前导0

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
typedef long long  type;
const int MAXN = 10005;

struct bign {
    int len, num[MAXN];

    bign () {
        len = 0;
        memset(num, 0, sizeof(num));
    }
    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}

    void DelZero ();
    void Put ();

    void operator = (int number);
    void operator = (char* number);

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

    void operator ++ ();
    void operator -- ();
    bign operator + (const type& b);
    bign operator + (const bign& b);
    bign operator - (const type& b);
    bign operator - (const bign& b);
    bign operator * (const type& b);
    bign operator * (const bign& b);
    bign operator / (const type& b);
    //bign operator / (const bign& b);
    int operator % (const int& b);
};

/*Code*/
int main () {
    int n;
    while (scanf("%d", &n) == 1) {
        bign ans = 0;
        bign tmp = 1;
        ans = ans + tmp;
        for (int i = 1; i <= n/2; i++) {
            tmp = tmp * 1LL * (n - 2 * i + 2) * (n - 2 * i + 1);
            tmp = tmp / (1LL * i * (i + 1));;
            ans = ans + tmp;
            ans.len = min(ans.len, 100);
        }
        ans.Put();
        printf("\n");
    }
}

void bign::DelZero () {
    while (len && num[len-1] == 0)
        len--;

    if (len == 0)
        num[len++] = 0;
}

void bign::Put () {
    bool flag = false;
    for (int i = len-1; i >= 0; i--) {
        if (num[i] || flag) {
            printf("%d", num[i]);
            flag = true;
        }
    }
}

void bign::operator = (char* number) {
    len = strlen (number);
    for (int i = 0; i < len; i++)
        num[i] = number[len-i-1] - ‘0‘;

    DelZero ();
}

void bign::operator = (int number) {

    len = 0;
    while (number) {
        num[len++] = number%10;
        number /= 10;
    }

    DelZero ();
}

bool bign::operator < (const bign& b) const {
    if (len != b.len)
        return len < b.len;
    for (int i = len-1; i >= 0; i--)
        if (num[i] != b.num[i])
            return num[i] < b.num[i];
    return false;
}

void bign::operator ++ () {
    int s = 1;

    for (int i = 0; i < len; i++) {
        s = s + num[i];
        num[i] = s % 10;
        s /= 10;
        if (!s) break;
    }

    while (s) {
        num[len++] = s%10;
        s /= 10;
    }
}

void bign::operator -- () {
    if (num[0] == 0 && len == 1) return;

    int s = -1;
    for (int i = 0; i < len; i++) {
        s = s + num[i];
        num[i] = (s + 10) % 10;
        if (s >= 0) break;
    }
    DelZero ();
}

bign bign::operator + (const type& b) {
    bign a = b;
    return *this + a;
}

bign bign::operator + (const bign& b) {
    type bignSum = 0;
    bign ans;

    for (int i = 0; i < len || i < b.len; i++) {
        if (i < len) bignSum += num[i];
        if (i < b.len) bignSum += b.num[i];

        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    while (bignSum) {
        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    return ans;
}

bign bign::operator - (const type& b) {
    bign a = b;
    return *this - a;
}


bign bign::operator - (const bign& b) {
    type bignSub = 0;
    bign ans;
    for (int i = 0; i < len || i < b.len; i++) {
        bignSub += num[i];
        bignSub -= b.num[i];
        ans.num[ans.len++] = (bignSub + 10) % 10;
        if (bignSub < 0) bignSub = -1;
        else bignSub = 0;
    }
    ans.DelZero ();
    return ans;
}

bign bign::operator * (const type& b) {
    type bignSum = 0;
    bign ans;

    ans.len = len;
    for (int i = 0; i < len; i++) {
        bignSum += num[i] * b;
        ans.num[i] = bignSum % 10;
        bignSum /= 10;
    }

    while (bignSum) {
        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }
    return ans;
}

bign bign::operator * (const bign& b) {
    bign ans;
    ans.len = 0; 

    for (int i = 0; i < len; i++){  
        int bignSum = 0;  

        for (int j = 0; j < b.len; j++){  
            bignSum += num[i] * b.num[j] + ans.num[i+j];  
            ans.num[i+j] = bignSum % 10;  
            bignSum /= 10;
        }  
        ans.len = i + b.len;  

        while (bignSum){  
            ans.num[ans.len++] = bignSum % 10;  
            bignSum /= 10;
        }  
    }  
    return ans;
}

bign bign::operator / (const type& b) {

    bign ans;

    type s = 0;
    for (int i = len-1; i >= 0; i--) {
        s = s * 10 + num[i];
        ans.num[i] = s/b;
        s %= b;
    }

    ans.len = len;
    ans.DelZero ();
    return ans;
}

int bign::operator % (const int& b) {

    bign ans;

    int s = 0;
    for (int i = len-1; i >= 0; i--) {
        s = s * 10 + num[i];
        ans.num[i] = s/b;
        s %= b;
    }

    return s;
}

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

标签:php   print   cat   names   pac   category   max   item   题目   

原文地址:http://www.cnblogs.com/lxjshuju/p/6800649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!