卡特兰数 HDU2067 & HDU4165 & HDU1134
题目链接:https://vjudge.net/problem/HDU-2067
小兔的棋盘
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11800 Accepted Submission(s): 5952
3
12
-1
2 3 10
3 12 416024
题解:
2.卡特兰数计算公式:
1) h(n) = h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (n>=1) , 其中 h[0] = 1;
2) h(n) = c(2n,n) - c(2n,n+1)(n=0,1,2,...) <==> h(n) = C(2n,n)/(n+1)
3) h(n) = h(n-1)*(4*n-2) / (i+1) ……此条计算公式容易溢出
注意:卡特兰数的计算很容易溢出。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; LL h[MAXN]; void init()
{
memset(h, , sizeof(h));
h[] = ; h[] = ;
for(int i = ; i<MAXN; i++)
for(int j = ; j<i; j++)
h[i] += 1LL*h[j]*h[i-j-];
} int main()
{
init();
int kase = , n;
while(scanf("%d", &n) && n!=-)
printf("%d %d %lld\n", ++kase, n, 2LL*h[n]);
}
题目链接: https://vjudge.net/problem/HDU-4165
Pills
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1626 Accepted Submission(s): 1139
On the first day, she removes a random pill, breaks it in two halves, takes one half and puts the other half back into the bottle.
On subsequent days, she removes a random piece (which can be either a whole pill or half a pill) from the bottle. If it is half a pill, she takes it. If it is a whole pill, she takes one half and puts the other half back into the bottle.
In how many ways can she empty the bottle? We represent the sequence of pills removed from the bottle in the course of 2N days as a string, where the i-th character is W if a whole pill was chosen on the i-th day, and H if a half pill was chosen (0 <= i < 2N). How many different valid strings are there that empty the bottle?
题解:
有n片药,每天吃半片。当天要么在药罐中抽到把一片完整的药片,然后分成两半,吃一半,最后把另一半放回药罐中;要么抽到半片药片直接吃。问:有多少种情况? 单纯的卡特兰数。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; LL h[MAXN]; void init()
{
memset(h, , sizeof(h));
h[] = ;
for(int i = ; i<MAXN; i++)
for(int j = ; j<i; j++)
h[i] += 1LL*h[j]*h[i-j-];
} int main()
{
init();
int n;
while(scanf("%d", &n) && n)
printf("%lld\n", h[n]);
}
题目链接:https://vjudge.net/problem/HDU-1134
Game of Connections
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5112 Accepted Submission(s): 2934
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?
题意:
1~2*n 顺时针排列成一圈, 用n条线段连接n对数,要求线段不能有交叉,问:有多少种连接情况?
题解:
可以将此题联想到出栈问题,这样就转化成卡特兰数了。
递推式一:
import java.util.Scanner;
import java.math.BigInteger; public class Main { public static void main(String[] args){ BigInteger[] a = new BigInteger[]; a[] = BigInteger.ONE;
for(int i=; i<=; i++) {
a[i] = BigInteger.valueOf();
for(int j=;j<i;j++){
a[i] = a[i].add(a[j].multiply(a[i-j-]));
}
} Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n=input.nextInt();
if(n==-) break;
System.out.println(a[n]);
}
}
}
递推式二:
import java.util.Scanner;
import java.math.BigInteger; public class Main { public static void main(String[] args){ BigInteger[] a = new BigInteger[]; a[] = BigInteger.ONE;
for(int i=; i<=; i++) {
a[i] = a[i-].multiply(BigInteger.valueOf(*i-)).divide(BigInteger.valueOf(i+));
} Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n=input.nextInt();
if(n==-) break;
System.out.println(a[n]);
}
}
}
卡特兰数 HDU2067 & HDU4165 & HDU1134的更多相关文章
- hdu2067 小兔的棋盘 DP/数学/卡特兰数
棋盘的一角走到另一角并且不越过对角线,卡特兰数,数据量小,可以当做dp求路径数 #include<stdio.h> ][]; int main() { ; ) { int i,j; lon ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- 卡特兰数(Catalan)
卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名,其前几项为 : 1, 2, ...
- NOIP2003pj栈[卡特兰数]
题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要性不言自明,任何 ...
- 卡特兰数 (Catalan)
卡特兰数:(是一个在计数问题中出现的数列) 一般项公式: 1. 或 2. 递归公式: 1. 或 2. 注:全部可推导. (性质:Cn为奇数时,必然出现在奇数项 2k- ...
- HDU 5673 Robot ——(卡特兰数)
先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和 ...
- HDU 1023 Traning Problem (2) 高精度卡特兰数
Train Problem II Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Sub ...
- HDU1130 卡特兰数
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- LightOJ1170 - Counting Perfect BST(卡特兰数)
题目大概就是求一个n个不同的数能构造出几种形态的二叉排序树. 和另一道经典题目n个结点二叉树不同形态的数量一个递推解法,其实这两个问题的解都是是卡特兰数. dp[n]表示用n个数的方案数 转移就枚举第 ...
随机推荐
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Array 模拟
题目链接:http://codeforces.com/contest/572/problem/A 题意 就给你两个数组,问你能不能从A数组中取出k个,B数组中取出m个,使得这k个都大于这m个. 题解 ...
- MQTT协议介绍
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.pdf MQTT 文档 http://mqtt.org/new/wp-c ...
- Cesium 显示CZML数据
转自文章 Cesium随笔(5)CZML介绍(介个文章是转的嘿嘿) 通过czml可以在cesium上实现非常棒的动态效果 CZML的结构 CZML是一种用来描述动态场景的JSON架构的语言,主 ...
- 邁向IT專家成功之路的三十則鐵律 鐵律十:IT人思維之道-跳脫框架
莊子的哲學思想歸本於老子,他認為人要解脫束縛必須做到不從任何的角度與任何的時間來看待事物,而是必須與天地同體,然而也唯有如此才能看清宇宙間萬事萬理的真諦.無論是莊子還是老子,他們畢竟是中國古代的聖賢, ...
- 何时才使用https访问项目
利用keytools生产证书,然后将证书导入到jvm和tomcat中,则访问该项目的时候就以https访问
- java性能监控工具jmc-windows
jmc Java Mission Control is a Profiling, Monitoring, and Diagnostics Tools Suite. Synopsis jmc [ opt ...
- hql 时间
1.hql中时间格式转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String d ...
- HDU 3435A new Graph Game(网络流之最小费用流)
题目地址:HDU 3435 这题刚上来一看,感觉毫无头绪. .再细致想想.. 发现跟我做的前两道费用流的题是差点儿相同的. 能够往那上面转换. 建图基本差点儿相同.仅仅只是这里是无向图.建图依旧是拆点 ...
- vs升级c++项目遇到的一些问题
1.error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x ...
- 数据挖掘 与 Web开发何去何从
(0)引子 以下以现实生活中的一个实例引出本博客的探究点.或许类似的情况正发生在你的身边. 小弟工作5年了,近期有点迷茫. 上一份工作在一家比較大的门户站点做web开发和移动互联网数据挖掘(人手比較紧 ...