卡特兰数 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个数的方案数 转移就枚举第 ...
随机推荐
- JavaScript 中那些关于坐标和距离的属性与方法
一 前言 在前端开发中总会遇到各种各样需要使用或计算坐标和距离的情况,但是这些属性和方法众多,全部熟练地记下来并非是一件易事,大多只能现查,耗费不少时间精力,于是便有了整理记录的想法,即加深了印象,又 ...
- mybatis ----SqlSessionManager
今天我们来看看这个类 有些写法还是很经典的 public class SqlSessionManager implements SqlSessionFactory, SqlSession { priv ...
- Android入门:MVC模式(中)
MVC 模式的最基本概念是分层设计,把我们的代码基于 View(视图).Model(模型).Controller(控制器)进行分类封装,这样做的目的是为了清晰结构,使代码更易维护和扩展. 在上一篇文章 ...
- php程序调试: xdebug的配置
怎样在phpeclipse中像调试Java程序一样调试php呢? XDebug的版本号非常多,打开http://xdebug.org/index.php.把站点细致看一下,你会发现有句"If ...
- Spin.js-CSS动画进度载入器
spin.js是一款很easy的CSS载入器,他是一款使用了VML(Vector Makeup Language)的CSS动画效果. spin.js的特性 他有着很强大的适应性.有着下面几个特性: 1 ...
- C# 操作摄像头
如有雷同,不胜荣幸,若转载,请注明 这个是大众普通方法,鉴于有网友和朋友问相同的问题.在这里将我的拙劣的代码关键部分贴出来.以便帮助很多其它的朋友们,不足之处甚多,我能够学习,交流,请教阁下 废话到此 ...
- Flash中如何使用滤镜
使用滤镜 应用或删除滤镜 复制和粘贴滤镜 为对象应用预设滤镜 启用或禁用应用于对象的滤镜 启用或禁用应用于对象的所有滤镜 创建预设滤镜库 对象每添加一个新的滤镜,在属性检查器中,就会将其添加到该对象所 ...
- JavaScript-4.7-friendly_table---ShinePans
<html> <head> <meta http-equiv="content-type" content="text/html;chars ...
- odoo税金处理
税金可以设置为'税金包含在价格中',或者'税金不包含在价格中'. 在税金计算处理过程中,odoo会将价格/金额按 total_included/ total_exincluded 分开 ...
- VS2010配置QT5.5.0开发环境
一.官网下载QT和qtvsaddin插件 网址:http://www.qt.io/download-open-source/ 1. 2. 3. 得到下载的安装包,点击安装就能够了 watermark/ ...