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 consid- ering 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 joinyour two friends, to share (with them) the solution totheir problem. “If we are 2n2n persons”, you said, “pickany 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 - 1n1 pairs of people, he/she can leave zero on the right and n - 1n1pairs on the left, 11 on the right and n - 2n2 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 C_nCn for nn pairs of people is given by:

 

\displaystyle C_n = C_{n - 1}C_0 + C_{n - 2}C_1 + ... + C_1C_{n-2}+C_0C_{n - 1}Cn=Cn1C0+Cn2C1+...+C1Cn2+C0Cn1

 

which, together with the fact that C_0 = C_1 = 1C0=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 C_nCn, namely:

 

\displaystyle C_n = \frac{\begin{pmatrix} 2n \\ n \end{pmatrix}}{n + 1}Cn=n+1(2nn)

 

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 C_nCn be the nnth Catalan number as defined above. By regarding the sequence (C_n)n \ge 0(Cn)n0 of Catalan numbers, we can define a sequence (S_n)n\ge 0(Sn)n0, corresponding to “squaring the Catalan sequence”, by considering the Cauchy product, or discrete convolution, of (C_n)n\ge 0(Cn)n0 with itself, i.e.,

 

\displaystyle S_n = \sum_{k = 0}^n C_k C_{n - k}Sn=k=0nCkCnk

 

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

Input Format

The input contains one line containing one non-negative integer: nn, with 0 \le n \le 5 0000n5000.

Output Format

Output a line containing S_nSn.

Hint

To see why (S_n)n\ge 0(Sn)n0 could be said to correspond to the square of the Catalan sequence we could look at Cauchy products of power series. Suppose that p(x) = \sum_{n = 0}^{\infty}a_nx^np(x)=n=0anxn and q(x) = \sum_{n = 0}^{\infty}b_nx^nq(x)=n=0bnxn, then p(x) \times q(x) = \sum_{n = 0}^{\infty}c_nx^np(x)×q(x)=n=0cnxn where c_n = \sum_{k = 0}^na_kb_{n - k}cn=k=0nakbnk

转载于:https://www.cnblogs.com/zllwxm123/p/9553550.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值