hdu 1130,hdu 1131(卡特兰数,大数)
How Many Trees?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3382 Accepted Submission(s): 1960
binary search tree is a binary tree with root k such that any node v
reachable from its left has label (v) <label (k) and any node w
reachable from its right has label (w) > label (k). It is a search
structure which can find a node with label x in O(n log n) average time,
where n is the size of the tree (number of vertices).
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?
2
3
import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
BigInteger [] h = new BigInteger[101];
h[1] = new BigInteger("1");
for(int i=2;i<=100;i++){
h[i] = h[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
}
Scanner sc =new Scanner (System.in);
while(sc.hasNext()){
int n =sc.nextInt();
System.out.println(h[n]);
}
}
}
hdu:1131 由n个带编号的结点组成二叉树的个数
思路:卡特兰数乘上编号的全排列
import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
BigInteger [] h = new BigInteger[101];
h[1] = new BigInteger("1");
for(int i=2;i<=100;i++){
h[i] = h[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
}
Scanner sc =new Scanner (System.in);
while(sc.hasNext()){
int n =sc.nextInt();
if(n==0)break;
System.out.println(h[n].multiply(fac(n)));
}
} private static BigInteger fac(int n) {
BigInteger sum = BigInteger.valueOf(1);
for(int i=1;i<=n;i++) sum=sum.multiply(BigInteger.valueOf(i));
return sum;
} }
hdu 1130,hdu 1131(卡特兰数,大数)的更多相关文章
- hdu 1130How Many Trees?(卡特兰数)
卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列. 以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为(从第零 ...
- HDU 1134 Game of Connections(卡特兰数+大数模板)
题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 20 ...
- 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- HDU 5673 Robot【卡特兰数】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如 ...
- HDU 5673 Robot ——(卡特兰数)
先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和 ...
- HDU 4828 Grids(卡特兰数+乘法逆元)
首先我按着我的理解说一下它为什么是卡特兰数,首先卡特兰数有一个很典型的应用就是求1~N个自然数出栈情况的种类数.而这里正好就对应了这种情况.我们要满足题目中给的条件,数字应该是从小到大放置的,1肯定在 ...
- hdu-1130(卡特兰数+大数乘法,除法模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1130 卡特兰数:https://blog.csdn.net/qq_33266889/article/d ...
- 【hdoj_1133】Buy the Ticket(卡特兰数+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...
随机推荐
- GCJ2008 APAC local onsites C Millionaire
自己Blog的第一篇文章,嗯... 接触这道题,是从<挑战程序设计竞赛>这本书看来的,其实头一遍读题解,并没有懂.当然现在已经理解了,想想当初可能是因为考虑两轮的那张概率图的问题.于是决定 ...
- https客户端遇到过的问题
1.用.p12格式的证书,在windows上调试完全没问题,在Linux服务器上,提示无效证书格式. 解决方法:将.p12格式的证书转换为.jks格式的证书. 将.p12格式的证书转换为.jks格式的 ...
- Codeforces Round #330 (Div. 2) B. Pasha and Phone
B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- ACM1258邻接表
教训:使用邻接表的时候一定要把邻接表的结构组定义的足够大,不能仅仅等于节点的个数,因为线段的数量往往远超过节点的数量. 这个题目是拓扑排序练习,提高下理解. #include<iostream& ...
- js ejs for语句的第二种遍历用法
var A = {a:1,b:2,c:3,d:"hello world"}; for(var k in A) { console.log(k,A[k]); var h = new ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [USACO11FEB] Cow Line
https://www.luogu.org/problem/show?pid=3014 题目描述 The N (1 <= N <= 20) cows conveniently number ...
- Maven -- 在进行war打包时用正式环境的配置覆盖开发环境的配置
我们的配置文件一般都放在 src/main/resource 目录下. 假定我们的正式环境配置放在 src/main/online-resource 目录下. 那么打成war包时,我们希望用onli ...
- sql cmd命令执行sqlserver的sql文件
有的时候,我们通过Log Explorer工具根据日志生成的回滚脚本,或者其他情况我们得到的脚本文件,通过sqlserver打开脚本文件的方式不爽,我们可以这样: 方式一: osql -S . -U ...
- jenkins slave agent 当作服务运行
1. 接上边编辑好文件 2. 双击以上的jnlp文件 3. 点击弹出的窗口File->save as service, 此时如果报错的话很可能是由于没有安装.net(.net2 以上) 4. 保 ...