特殊函数卡特兰数 hdu 1134

Game of Connections

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

Total Submission(s): 3352    Accepted Submission(s): 1910

Problem 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

Asia 2004, Shanghai (Mainland China), Preliminary

题目大意:2n个连线,不能相交,求最多有多少种可能的握手方法?

考察知识点:大数,卡特兰数

//考察知识点:卡特兰数 && 大数。
/*
h(1)=1,h(0)=1;h(n)=((4*n-2)/(n+1))*h(n-1);(其中n>=2)
*/
#include<stdio.h>
#include<string.h>
#define inf 110
int a[inf][inf];
int i,j,temp,len,n;
void f()
{
	temp=0;len=1;
	memset(a,0,sizeof(a));
	a[1][0]=1;
	for(i=2;i<=100;++i)
	{
		for(j=0;j<len;++j)
		{
			a[i][j]=a[i-1][j]*(4*i-2);
		}
		for(j=0;j<len;++j)
		{
			temp+=a[i][j];
			a[i][j]=temp%10;
			temp/=10;
		}
		while(temp)
		{
			a[i][len++]=temp%10;
			temp/=10;
		}
		//len--;
		for(j=len-1;j>=0;--j)
		{
			temp=temp*10+a[i][j];
			a[i][j]=temp/(i+1);
			temp%=i+1;
		}
		while(!a[i][len-1])
		len--;
	}
}
int main()
{
	f();
	while(~scanf("%d",&n),n!=-1)
	{
		for(i=inf-1;i>=0&&!a[n][i];--i);
		for(;i>=0;--i)
		printf("%d",a[n][i]);
		puts("");
	}
	return 0;
}
 
时间: 2024-08-18 23:48:27

特殊函数卡特兰数 hdu 1134的相关文章

HDU 1134 Game of Connections(卡特兰数)

题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4668    Accepted Submission(s): 2729 Problem Description Thi

hdu 5177 (1e18范围的卡特兰数)

hdu 5177 (1e18范围的卡特兰数) 题意: 求第n个卡特兰数,模3814697265625 (5^18) 限制: 有20组数据,1 <= n <= 1e18 思路: 1. 卡特兰数的表达式: ans = 1/(n+1) * C(2*n,n) -> ans = 1/(n+1) * (2n)! / n! / n!    ---1式 2. 因为要模5^18,求逆元要求互质,所以先把"1式"中的因子5全部去掉 3. 然后看不含因子5的阶乘,f(n!) 4. 设g(x

hdu 4828 Grids(拓展欧几里得+卡特兰数)

题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1000005; const ll MOD = 1e9+7; ll dp[N]; ll extendGcd(ll a, ll b, ll& x, ll& y) { if (

2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆

【HDU 5370】 Tree Maker(卡特兰数+dp)

Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting game which is named Tree Maker. In this game, all trees are binary trees. Initially, there is a tree with only one vertex and a cursor on it. Tree Lover

HDU 5673 Robot 卡特兰数

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题目描述: 一个人从原点开始向右走, 要求N秒后回到原点, 且过程中不能到负半轴, 人有两种操作, 走动或者停止, 问总共有多少种方案? 解题思路: 类似于括号匹配问题, 和那个我去年这个时候接触到的最裸的不能越过对角线的正方形走到对角问题, 卡特兰数, 从2开始枚举走动步数, 然后剩下的就是不动的步数, 用不动的步数做个填充就可以了, 设计到取模, 需要逆元 代码: #include <i

LA 5092 &amp;&amp; hdu 3723 Delta Wave (卡特兰数)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3723 and http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20568 题意:有种折线每向右延伸一个单位长度,高度要么不变,要么加1,要么减1.而且任何时刻高度不能低于0.求这种折线最终高度为0的情况总数. 分析:由于任何时刻斜向上的线不小于斜向下的线的数目,而且最终相等,,,,,卡特兰数模型.卡特兰数资料 若有i条斜向上的线,那

HDU 4828 (卡特兰数+逆元)

HDU 4828 Grids 思路:可以转化为卡特兰数,先把前n个人标为0,后n个人标为1,然后去全排列,全排列的数列,如果每个1的前面对应的0大于等于1,那么就是满足的序列,如果把0看成入栈,1看成出栈,那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了.然后去递推一下解,过程中需要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long M

hdu 5184 卡特兰数

hdu 5184 卡特兰数 题意: 我们给出下列递归的合法括号序列的定义: 1. 空序列是合法括号序列 2. 如果s是一个合法括号序列,那么(s)也是合法括号序列 3. 如果a和b是合法括号序列,那么ab也是合法括号序列 4. 没有其它情况是合法括号序列 比如下列括号序列是合法括号序列 (), (()), ()(), ()(()) 下列括号序列则不是 (, ), )(, ((), ((() 现在,我们要构造长度为n的合法括号序列,前面的一些括号已经给出,问可以构造出多少合法序列. 限制: 1 <