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 - 1n−1 pairs of people, he/she can leave zero on the right and n - 1n−1pairs on the left, 11 on the right and n - 2n−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 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=Cn−1C0+Cn−2C1+...+C1Cn−2+C0Cn−1
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{egin{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)n≥0 of Catalan numbers, we can define a sequence (S_n)nge 0(Sn)n≥0, corresponding to “squaring the Catalan sequence”, by considering the Cauchy product, or discrete convolution, of (C_n)nge 0(Cn)n≥0 with itself, i.e.,
displaystyle S_n = sum_{k = 0}^n C_k C_{n - k}Sn=k=0∑nCkCn−k
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 0000≤n≤5000.
Output Format
Output a line containing S_nSn.
Hint
To see why (S_n)nge 0(Sn)n≥0 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=0∞anxn and q(x) = sum_{n = 0}^{infty}b_nx^nq(x)=∑n=0∞bnxn, then p(x) imes q(x) = sum_{n = 0}^{infty}c_nx^np(x)×q(x)=∑n=0∞cnxn where c_n = sum_{k = 0}^na_kb_{n - k}cn=∑k=0nakbn−k.
样例输入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 }