UVA 10303 How Many Trees?
来源:互联网 发布:供电公司内网网络大学 编辑:程序博客网 时间:2024/06/06 03:56
题意:设f[i]表示一共有i个元素时二叉搜索树的个数,那么依次取1~n-1作为根节点,那么左右子树元素的个数就分别是(0,n-1),......,所以f[n] = f[0]*f[n-1]+f[1]*f[n-2]...+f[n-1]f[0],其实也就是Catalan数,高精度的计算,递推公式是f[n]=(4n-2)/(n+1)*f[n-1]
#include <iostream>#include <cstring>#include <cmath>#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 4000;int f[MAXN][MAXN];int l[MAXN];void div(int x){ int *t = f[x],y=x+1,cur=0; for (int i = l[x]-1; i >= 0; i--){ cur = cur*10+t[i]; t[i] = cur / y; cur %= y; } int j; for (j = l[x]-1; j > 0 && !t[j]; j--); l[x] = j + 1; }void mul(int x){ int &i = l[x],c=0,y=4*x-2; int *p=f[x-1],*t=f[x]; for (i = 0; i < l[x-1] || c; i++){ c += p[i] * y; t[i] = c % 10; c /= 10; }}void print(int x){ int *p=f[x]; for (int i = l[x]-1; i >= 0; i--) printf("%d",p[i]); printf("\n");}int main(){ int n; memset(f,0,sizeof(f)); f[1][0] = 1; l[1] = 1; for (int i = 2; i <= 1000; i++){ mul(i); div(i); } while (scanf("%d",&n) != EOF){ print(n); } return 0;}
- Uva 10303 How Many Trees?
- UVA 10303 How Many Trees?
- UVA 10303 How Many Trees?
- UVa:10303 How Many Trees ?
- uva 10303 How Many Trees?
- UVa 10303 How Many Trees? (卡特兰数&高精度)
- uva 10303 - How Many Trees?(卡特兰数)
- UVA 10303 - How Many Trees?(数论 卡特兰数 高精度)
- UVa10303 - How Many Trees?
- Codeforces_9D-How many trees?
- How Many Trees?
- hdu1130-How Many Trees?
- How Many Trees?
- How Many Trees?
- How Many Trees?
- HDU How Many Trees?
- hdu 1130 How Many Trees?
- HDU 1130 How Many Trees?
- C#高级编程读书笔记 - 第一章.Net体系结构
- SQL Server中的临时表的应用
- 业务分析规划设计和软件程序开发实施
- hdu 4251 The Famous ICPC Team Again(划分树裸题)
- javascript推荐书籍
- UVA 10303 How Many Trees?
- Java程序员到架构师的推荐阅读书籍
- 如何高效使用Vim
- J2EE架构师之路
- Java Object的hashCode()和equals()简单分析
- 新西游记游戏
- 山寨腾讯“爱消除”游戏7日教程--DAY3
- Failed to load class "org.slf4j.impl.StaticLoggerBinder"问题
- 小明系列故事——买年货