HDU 1130 How Many Trees卡特兰数
来源:互联网 发布:电脑室控制软件 编辑:程序博客网 时间:2024/05/23 14:22
How Many Trees?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3943 Accepted Submission(s): 2232
Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?
123
125
#include<iostream>#include<cstring>using namespace std;#define maxn 110#define base 10000void multiply(int a[],int b){//大数乘法 int i,temp=0; for(i=maxn-1;i>=0;i--){ temp+=b*a[i]; a[i]=temp%base; temp/=base; }}void divide(int a[],int b){//大数除法 int i,temp=0; for(i=0;i<maxn;i++){ temp=temp*base+a[i]; a[i]=temp/b; temp%=b; }}int main(){ int a[maxn][maxn],i,n; memset(a,0,sizeof(a)); for(i=2,a[1][maxn-1]=1;i<maxn;i++){//高坐标存放大数低位 memcpy(a[i],a[i-1],maxn*sizeof(int)); multiply(a[i],4*i-2); divide(a[i],i+1); } while(cin>>n){ for(i=0;i<maxn&&a[n][i]==0;i++); cout<<a[n][i++]; for(;i<maxn;i++){ printf("%04d",a[n][i]); } cout<<endl; } return 0;}
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 110
int a[maxn][maxn];//a[maxn][maxn]用来大数处理
int b[maxn];//b[maxn]用来存储位数
void catalan(){//第一种,也就是本题的AC代码
int i,j,len,carry,temp;//carry处理进位,temp用来暂时存储进位的数+原数
a[1][0]=b[1]=1;
len=1;
for(i=2;i<=100;i++){
for(j=0;j<=len;j++)
a[i][j]=a[i-1][j]*(4*(i-1)+2);
carry=0;
for(j=0;j<len;j++){
temp=a[i][j]+carry;
a[i][j]=temp%10;
carry=temp/10;
}
while(carry){
a[i][len++]=carry%10;
carry/=10;
}
carry=0;
for(j=len-1;j>=0;j--){
temp=a[i][j]+carry*10;
a[i][j]=temp/(i+1);
carry=temp%(i+1);
}
while(!a[i][len-1])
len--;
b[i]=len;
}
}
int main(){
catalan();
int i,n;
while(cin>>n){
for(i=b[n]-1;i>=0;i--)
cout<<a[n][i];
cout<<endl;
}
return0;
}
方法二:
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 110
#define base 10000
void multiply(int a[],int b){//大数乘法
int i,temp=0;
for(i=maxn-1;i>=0;i--){
temp+=b*a[i];
a[i]=temp%base;
temp/=base;
}
}
void divide(int a[],int b){//大数除法
int i,temp=0;
for(i=0;i<maxn;i++){
temp=temp*base+a[i];
a[i]=temp/b;
temp%=b;
}
}
int main(){
int a[maxn][maxn],i,n;
memset(a,0,sizeof(a));
for(i=2,a[1][maxn-1]=1;i<maxn;i++){//高坐标存放大数低位
memcpy(a[i],a[i-1],maxn*sizeof(int));
multiply(a[i],4*i-2);
divide(a[i],i+1);
}
while(cin>>n){
for(i=0;i<maxn&&a[n][i]==0;i++);
cout<<a[n][i++];
for(;i<maxn;i++){
printf("%04d",a[n][i]);
}
cout<<endl;
}
return 0;
}
- HDU 1130 How Many Trees? 卡特兰数
- HDU 1130 how many trees(卡特兰数)
- HDU 1130 How Many Trees?(卡特兰数+大数)
- HDU 1130 How Many Trees卡特兰数
- hdoj-1130-How Many Trees?【卡特兰数】
- hdoj 1130 How Many Trees? 【卡特兰数】
- hdu 1130 How Many Trees?(卡特兰数,大数的乘法与除法)
- ACMSTEP 2.3.5 How Many Trees? //高精度卡特兰数
- UVa 10303 How Many Trees? (卡特兰数&高精度)
- uva 10303 - How Many Trees?(卡特兰数)
- HDOJ 1130 How Many Trees?(卡特兰数+大数乘除法)
- 【HDU】1130 - How Many Trees?(java - BigDecimal & 卡特兰大数打表)
- hdu1130How Many Trees? 卡特兰数
- (卡特兰数,二叉树数量)How Many Trees? &&Count the Trees -- HDOJ
- UVA 10303 - How Many Trees?(数论 卡特兰数 高精度)
- hdu 1131 Count the Trees(n!*卡特兰数)
- HDU 3240 Counting Binary Trees 数论-卡特兰数
- HDU 1131Count the Trees(卡特兰数 大数)
- HDU1405 The Last Practice
- linux中硬链接与软链接的区别与联系
- iOS开发中系统的UITableViewCell只有当有数据的时候显示分割线
- 外网ip注册dubbo服务
- Jedis
- HDU 1130 How Many Trees卡特兰数
- HTTP状态码查询手册
- linux shell命令查看接口索引--ip link show
- GLSL语言基础
- Android常见内存泄漏案例
- Python安装模块出错(ImportError: No module named setuptools)解决方法
- Concepts Nrec MAX-PAC v8.2.4.24 Win64 1CD(叶轮零件整体数控加工自动数控编程)
- SSM
- 扩展屏幕没有连接,但程序窗口还在扩展屏幕上,在看不到这个屏幕的情况下,把程序窗口拉回来的方法