您的位置:首页 > 其它

hdu 2067 小兔的棋盘(卡特兰数)

2015-03-30 20:16 411 查看

小兔的棋盘

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7277 Accepted Submission(s): 3884



Problem Description
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!


Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。


Output
对于每个输入数据输出路径数,具体格式看Sample。


Sample Input
1
3
12
-1




Sample Output
1 1 2
2 3 10
3 12 416024




Author
Rabbit


Source
RPG专场练习赛

题目分析:这道题的行和列可以分别看做是进栈和出栈,因为不能越过对角线,所以必须先入栈才能出栈,那么就变成了卡特兰数的模型

package hdu;

import java.util.Scanner;
import java.math.BigInteger;

public class Main 
{
	static final int MAX = 107;
	static BigInteger [][]c = new BigInteger [MAX*2][MAX*2];
	
	static void init ( )
	{
		c[1][1] = c[0][0] = c[1][0] = BigInteger.ONE;
		for ( int i = 2 ; i < MAX*2 ; i++ )
			for ( int j = 0 ; j <= i ; j++ )
				if ( j == 0 || j == i ) c[i][j] = BigInteger.ONE;
				else c[i][j] = c[i-1][j].add(c[i-1][j-1]);
	}
	
	public static void main ( String args[] )
	{
		Scanner cin = new Scanner ( System.in );
		int n,m;
		init ();
		int cc = 1;
		while ( cin.hasNext() )
		{
			n = cin.nextInt();
			if ( n == -1 ) break;
			System.out.println (cc++ +" "+n+" " + c[2*n]
.divide(BigInteger.valueOf(n+1) ).multiply(BigInteger.valueOf(2))  );
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: