HDU 4165 卡特兰】的更多相关文章

题意:有n个药片,每次吃半片,吃2n天,那么有多少种吃法. 分析:如果说吃半片,那么一定要吃过一整片,用 ) 表示吃半片,用 ( 表示吃整片,那么就是求一个正确的括号匹配方案数,即卡特兰数. 卡特兰数可以DP,也有一重循环的地推表达式:Catalan(n) = 2(2n-1)*Catalan(n-1)/(n+1) #include <bits/stdc++.h> using namespace std; ]={,,,}; int main() { //freopen("in.txt&…
HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看成出栈.那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了. 然后去递推一下解,过程中须要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long…
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 Problem Description A binary search tree is a binary tree with root k such that any node v re…
一块药看成括号配对就行了.很明显的直接求卡特兰数. 今晚看了HDU 3240的题,有一点思路,但无情的TLE.想不到什么好方法了,看了别人的解答,哇...简直是天才的做法啊....留到星期六自己思考一遍再重新写. import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; import java.io.InputStreamReader; public class Main{ public…
HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0,后n个人标为1.然后去全排列,全排列的数列,假设每一个1的前面相应的0大于等于1,那么就是满足的序列.假设把0看成入栈,1看成出栈.那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了.然后去递推一下解,过程中须要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long…
可以用卡特兰数做 以下分析转自:http://www.cnblogs.com/kevinACMer/p/3724640.html?utm_source=tuicool 这道题之前自己做的时候并没有反应过来是求卡特兰数,当时是按递推来想的.后来查了下HDU4165,结果一看大标题就说是卡特兰数,自己想了想, 还真是那么回事.主要还是对于卡特兰数用的不多,也就当时没立马反应过来了.下面介绍这道题我的思路,然后对卡特兰数再做一些补充. 本题题意:罐子里有N片相同的药片,开始的时候药片都是完整的一整片.…
Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasi…
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆…
题意是求一列连续升序的数经过一个栈之后能变成的不同顺序的数目. 开始时依然摸不着头脑,借鉴了别人的博客之后,才知道这是卡特兰数,卡特兰数的计算公式是:a( n )  =  ( ( 4*n-2 ) / ( n+1 ) * a( n-1 ) ): 用一个二维数组,a[ i ][ 0 ] 表示第 i 个卡特兰数的位数,a[ i ][ j ] ( j != 0) 中存第 i 个卡特兰数从低位到高位的第 j 个数,也就是说数是倒过来存的,输出时要倒着输出. 代码如下: #include<bits/stdc…
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you? Suppose the cinema only has one ticket-office and…
  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133   题意:排队买50块一张的票,初始票台没有零钱可找,有m个人持有50元,n人持有100元,每人编号各不相同.问有多少种排队方案? 题解: 当 m<n时,肯定方案数是0. 当m>=n时,将队伍看成一个栈,持有50的人用0表示,持有100的人用1表示. 对于n+m个数我们能有的总方案数有C(n+m,n)种. 不符合的方案数:(以下是百度百科的解释) 考虑一个含n个1.n个0的2n位二进制数…
题意:每个人有一个DI值,现在有一个小黑屋,这些人的顺序可以利用这个小黑屋调整,调整方式是入栈出栈方式,也就是说,这里的方案是有卡特兰数个方式. 调整后使得 d1*0 + d2*1 + d3*2 + d4*3 ...... 最小. 分析:这个题目竟然会是区间DP. 考虑区间 [ L, R ] ,那么L,可以从任意位置出栈,枚举出栈位置,可以划分为两个部分,也就是说两个子问题,但是如何利用这两个子问题得到 d[L,R], d[L,R] = 前一部分 + D[L]*(i-L) + 后一部分 + 后一…
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.   Input The input contains…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5566    Accepted Submission(s): 2326 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex…
Pills Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Aunt Lizzie takes half a pill of a certain medicine every day. She starts with a bottle that contains N pills. On the first day, she removes…
Problem Description This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them…
Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5372    Accepted Submission(s): 2911 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stati…
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与第n-1项有关系,上网搜索的时候发现是组合数学中关于Catalan(卡特兰)数的运用. 以下资料来自网络,整理以备记录学习: [来源链接] http://baike.baidu.com/view/2499752.htm http://blog.163.com/lz_666888/blog/stati…
题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1000005; const ll MOD = 1e9+7; ll dp[N]; ll extendGcd(ll a, ll b, ll& x, ll& y) { if (…
Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting game which is named Tree Maker. In this game, all trees are binary trees. Initially, there is a tree with only one vertex and a cursor on it. Tree Lover…
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6084 [题目大意] 对于一个串S,当它同时满足如下条件时,它就是一个01偏串: 1.只由0和1两种符组成: 2.在S的每一个前缀中,0的个数不超过1的个数: 3.S中0的个数和1的个数相等. 现在给定01偏串S,请计算一下S在所有长度为n的01偏串中作为子串出现的次数的总和. 由于结果比较大,结果对1e9+7取余后输出. [题解] 我们发现01偏串实际上等价于合法括号序列, 在合法括号序列中取出…
题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4668    Accepted Submission(s): 2729 Problem Description Thi…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如果机器人的当前位置在原点右侧,它同样可以向左移动单位距离.一系列的移动(左移,右移,原地不动)定义为一个路径.问有多少种不同的路径,使得n秒后机器人仍然位于坐标原点?答案可能很大,只需输出答案对1,000,000,007取模. 分析: 最终回到原点说明向左和向右走的步数相同,假设一共走了i步,那么左…
先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和出栈的次数都是n次,问最后栈空的合法序列的个数.其他例子见上面这个博客. 那么关于这个题目,我们先选出i次右移的(相当于进栈)次数,i次左移的(相当于出栈)次数,那么当前对答案做出的贡献就是C(n,2*i)*cat[i],枚举所有的i计算出答案即可. 代码如下: #include <stdio.h>…
Train Problem II Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing…
Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description There is a robot on the origin point of an axis.Every second, the robot can move right one unit length or do nothing.If the robot is on the…
Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem Description 度度熊最近很喜欢玩游戏.这一天他在纸上画了一个2行N列的长方形格子.他想把1到2N这些数依次放进去,但是为了使格子看起来优美,他想找到使每行每列都递增的方案.不过画了很久,他发现方案数实在是太多了.度度熊想知道,有多少种放数字的方法能满足上面的条件?   Input 第一行为数…
题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #include <bits/stdc++.h> using namespace std; ; vector<string> vect; void _mult(string num1, string num2, string &result ) { reverse(num1.begin()…
Brackets Problem Description We give the following inductive definition of a “regular brackets” sequence:● the empty sequence is a regular brackets sequence,● if s is a regular brackets sequence, then (s) are regular brackets sequences, and● if a and…
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you? Suppose the cinema only has one tic…