卡特兰数整理

卡特兰数看了整整一天,其实思想好理解,递推关系f(n)=f(0)f(n-1)+f(1)f(n-2)+f(2)f(n-3),,,,,f(n-1)f(0);

 

 

 

 

我是这么理解的:假设1,2,3,,,,,n个数出栈,入栈,出栈的方式为f(n),假设第k个数最后出栈,序列分别1—k-1,k+1—n两个独立的序列,根据乘法原理,f(n)=f(k-1)*f(n-k);又因为k可以取1——n;根据加法原理,即可得f(n)=f(0)f(n-1)+f(1)f(n-2)+f(2)f(n-3),,,,,f(n-1)f(0);

 

 

 

 

 

原本不需要花这么长时间看的,但看大神们的博客的时候,说卡特兰数与66种组合数相关,就都看了一下;比较有意思的是买票问题:一张票5元,有8个人有5元,8个人有10元,开始售票员没有钱,每人买一张票,求不发生售票员找不开钱的情况有多少种?

 

 

 

 

 

 

也是卡特兰数的变形,把有5元的看成1,10元的看成0,等价于n个1,n个0组成的2n为二进制数,从左往右,1的个数>=0的个数。从2n个位置填入n个1的方法有C(2n,n)种,其他位置自动填0;从C(2n,n)中减去不合法的即可;不合法发生在某奇数位2m+1上,前面有m+1个0,m个1;

 

 

 

 

 

 

而此后的2n-2m-1个位置上,会有n-m个1,n-m-1个0如果这后面2n-2m-1的位置上0,1位置互换,即有n-m个0,n-m-1个1;结果得n+1个0,和n-1个1组成的2n为二进制数,所以一个不合法的数对应一个n-1个1,n+1个0组成的一个排列;

 

 

 

 

 

 

反过来,任何一个有n+1个0,n-1个1组成的2n位二进制,因为0个个数多两个,所以会在某一奇数位2m+1上出现0的统计个数>1的个数,前面有m+1个0,m个1;而此后的2n-2m-1个位置上,会有n-m-1个1,n-m个0同样后面的0,1互换,这样就组成了一个由n个1,n个0组成的2n位二进制数!而此序列是不合法的。即n+1个1,n-1个0组成的2n位数比对应一个不合法的n个1,n个0组成的2n位数!

 

 

 

 

 

 

 

例如:11000100

对应:11000011

在第5位出现异常,此序列不合法!

所以f(n)=C(2n,n)-C(2n,n+1)

此题的结果为:(C(16,8)-C(16,9))*8!*8!(前后两个8是,5元,10元人的全排列)

 

 

 

 

 

卡特兰数的另类递推公式:

h(n)=h(n-1)*(4*n-2)/h(n-1)

h(n)=C(2n,n)/(n+1);

 

 

 

 

 

 

昨天坐题时,大数的乘法,除法得c语言不会写,看大神的代码看了一天也不懂,主要是自己太渣渣了!今天发现用Java写so easy!当然,也是从大神那里学的,纪念一下!

 

下面两道类似,改一下就可以,其实所有的卡特兰数的代码都类似,懂了就可以

poj 2084

Description

This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another.
And, no two segments are allowed to intersect.
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

Input

Each line of the input file will be a single positive number n, except the last line, which is a number -1.
You may assume that 1 <= n <= 100.

Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

Sample Input

2
3
-1

Sample Output

2
5

Source

 

hdu 1023

Train Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6267    Accepted Submission(s): 3399


Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
 

Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
 

Output
For each test case, you should output how many ways that all the trains can get out of the railway.
 

Sample Input
   
   
1 2 3 10
 

Sample Output
   
   
1 2 5 16796
Hint
The result will be very large, so you may not process it by 32-bit integers.

import java.util.*;
import java.io.IOException;
import java.math.*;
public class Main {

	public static void main(String[] args)throws IOException{
		int i,n;
		Scanner sc=new Scanner(System.in);
		BigInteger h[]=new BigInteger[105];
		h[0]=BigInteger.ONE;
		h[1]=BigInteger.ONE;
		for(i=2;i<=100;i++)
			h[i]=h[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
		while(sc.hasNext()){
			n=sc.nextInt();
			if(n==-1)
				break;
			System.out.println(h[n]);
		}
	}
}

 

 

 

 

 

 

 

 

 

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值