hdu 2067 小兔的棋盘
http://acm.hdu.edu.cn/showproblem.php?pid=2067
问题描述:卡特兰数的应用
http://www.cppblog.com/MiYu/archive/2010/08/07/122573.html
卡特兰数的递推及通项公式:
h(n) = h(0)h(n-1) + h(1)h(n-2)+……+h(n-1)h(0)
h(n) = h(n-1) × (4n-2)/(n+1)
h(n) = C(n,2n) - C(n+1,2n) = C(n,2n)/(n+1)
思路
n<=35的卡特兰数用long long 过的掉,h[35]约为6X10^18左右
参考代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
int n;
ll h[40];//1-35个卡特兰数
void init(){
// fill(f,f+40,0LL);
h[0] = 1;
for(int i = 1; i <= 35; ++ i){
h[i] = 0;
for(int j = 0; j <= i - 1;++ j)
h[i]+=h[j]*h[i-j-1];
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
int cnt = 1;
init();
while(scanf("%d",&n) == 1 && n!= -1){
printf("%d %d %lld\n",cnt++,n,2*h[n]);
}
return 0;
}
- 加粗
Ctrl + B
- 斜体
Ctrl + I
- 引用
Ctrl + Q
- 插入链接
Ctrl + L
- 插入代码
Ctrl + K
- 插入图片
Ctrl + G
- 提升标题
Ctrl + H
- 有序列表
Ctrl + O
- 无序列表
Ctrl + U
- 横线
Ctrl + R
- 撤销
Ctrl + Z
- 重做
Ctrl + Y