Catalan Square (卡特兰数 大数)

Last weekend you and your friends went to visit the local farmer’s market at the town square. As you were standing around in a circle talking, you couldn’t help overhearing two of your friends musing over what sounded like an interesting problem: They were considering the number of ways in which you could all shake hands, such that everyone in the circle simultaneously shaked hands with one other person, but where no arms crossed each other.

After a few seconds’ thought you decided to join your two friends, to share (with them) the solution to their problem. “If we are 2n persons”, you said, “pick any particular person, and let that person shake hands with somebody. That person will have to leave an even number of people on each side of the person with whom he/she shakes hands. Of the remaining n − 1 pairs of people, he/she can leave zero on the right and n − 1 pairs on the left, 1 on the right and n − 2 pairs on the left, and so on. The pairs remaining on the right and left can independently choose any of the possible non-crossing handshake patterns, so the count Cn for n pairs of people is given by:

                             Cn = Cn−1C0 + Cn−2C1 + . . . + C1Cn−2 + C0Cn−1,

which, together with the fact that C0 = C1 = 1, is just the definition of the Catalan numbers.” By consulting your handy combinatorics book, you find out that there is a much more efficient formula for calculating Cn, namely:

                              Cn = C(2n,n)/ ( n + 1).

After a collective groan from the group, your particularly cheeky friend Val called out “Well, since we are at the town square, why don’t you try to square your Catalan numbers!”. This was met with much rejoicing, while you started to think about how to square the Catalan sequence. . .

Let Cn be the nth Catalan number as defined above. By regarding the sequence (Cn)n≥0 of Catalan numbers, we can define a sequence (Sn)n≥0, corresponding to “squaring the Catalan sequence”, by considering the Cauchy product, or discrete convolution, of (Cn)n≥0 with itself, i.e.,

                             

Your task is to write a program for calculating the number Sn. 1

Input The input contains one line containing one non-negative integer: n, with 0 ≤ n ≤ 5 000. Output Output a line containing Sn. Sample Input 1

0

Sample Output

1

 Sample Input 2

59

Sample Output 2

1583850964596120042686772779038896

题意:求 卡特兰数的第 n+1 项。

昨晚刚看题时扫了一眼,没怎么看,快结束时,又看这道题,才发现是卡特兰数的板子,正好前几天大佬给了我一份卡特兰数的板子,我就和大佬说,这道题是卡特兰数,大佬回答我说“我用大数板子试了,超时!”,瞬间吓的我把敲了一般的大数板子给扔了,打开JAVA用java写,结果TLE,后来才知道大佬不知道这是卡特兰数的板子题,原因是翻译的队友不知道,只和他大概翻译了一下,然后他用大数模拟TLE...Orz...

代码如下:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<iostream>
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1e9+7
using namespace std;
#define MAX 1010
#define BASE 10000
const double pi=acos(-1.0);
int a[5010][MAX];
void multiply(int a[],int Max,int b)
{
    int i,array=0;
    for(i=Max-1; i>=0; i--)
    {
        array+=b*a[i];
        a[i]=array%BASE;
        array/=BASE;
    }
}
void divide(int a[],int Max,int b)
{
    int i,div=0;
    for(i=0; i<Max; i++)
    {
        div=div*BASE+a[i];
        a[i]=div/b;
        div%=b;
    }
}
int main()
{
    int i,j,n;
    for(i=2,a[1][MAX-1]=1; i<=5001; i++)
    {
        memcpy(a[i],a[i-1],sizeof(a[i-1]));
        multiply(a[i],MAX,4*i-2);
        divide(a[i],MAX,i+1);
    }
    while(~scanf("%d",&n))
    {
        n++;
        for(i=0; i<MAX&&a[n][i]==0; i++);
        printf("%d",a[n][i++]);
        for(; i<MAX; i++)
            printf("%04d",a[n][i]);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值