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.

样例输入1

0

样例输出1

1

样例输入2

59

样例输出2

1583850964596120042686772779038896


卡特兰数.


计算公式

卡特兰数一般的计算公式:这里写图片描述 

 


另类递推公式:C(n)=C(n-1)*((4*n-2)/(n+1));


s(n) = c(n+1);

 1 import java.math.BigInteger;
 2 import java.util.*;
 3 import java.util.Scanner;
 4 public class Main{
 5     BigInteger[] cal = new BigInteger[10005];
 6     void getval(){
 7         cal[0] = BigInteger.ZERO;
 8         cal[1] = BigInteger.ONE;
 9         for(int i=2;i<10005;i++){
10             cal[i] =cal[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
11         }
12     }
13 
14     Main(){
15         Scanner cin = new Scanner(System.in);
16         int n = cin.nextInt();
17         getval();
18         System.out.println(cal[n+1]);
19     }
20     public static void main(String []args){
21         new Main();
22     }
23 }

 

posted @ 2018-08-29 12:18  #忘乎所以#  阅读(283)  评论(0编辑  收藏  举报