既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法...

有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  ///

折纸法证明卡特兰数: http://blog.sina.com.cn/s/blog_6917f47301010cno.html

Brackets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 506    Accepted Submission(s): 120

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 b are regular brackets sequences, then ab is a regular brackets sequence.
● no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:
(), (()), ()(), ()(())
while the following character sequences are not:
(, ), )(, ((), ((()

Now we want to construct a regular brackets sequence of length n, how many regular brackets sequences we can get when the front several brackets are given already.

Input
Multi test cases (about 2000), every case occupies two lines.
The first line contains an integer n.
Then second line contains a string str which indicates the front several brackets.

Please process to the end of file.

[Technical Specification]
1≤n≤1000000
str contains only '(' and ')' and length of str is larger than 0 and no more than n.

Output
For each case。output answer % 1000000007 in a single line.
Sample Input
4 () 4 ( 6 ()
Sample Output
1 2 2

Hint

For the first case the only regular sequence is ()(). For the second case regular sequences are (()) and ()(). For the third case regular sequences are ()()() and ()(()).

/* ***********************************************
Author        :CKboss
Created Time  :2015年03月18日 星期三 20时10分21秒
File Name     :HDOJ5184.cpp
************************************************ */#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>using namespace std;typedef long long int LL;const int maxn=1001000;
const LL mod=1000000007LL;int n,len;
char str[maxn];LL inv[maxn];
LL jc[maxn],jcv[maxn];void init()
{inv[1]=1; jc[0]=1; jcv[0]=1;jc[1]=1; jcv[1]=1;for(int i=2;i<maxn;i++){inv[i]=inv[mod%i]*(mod-mod/i)%mod;jc[i]=(jc[i-1]*i)%mod;jcv[i]=(jcv[i-1]*inv[i])%mod;}
}LL COMB(LL n,LL m)
{if(m<0||m>n) return 0LL;if(m==0||m==n) return 1LL;LL ret=((jc[n]*jcv[n-m])%mod*jcv[m])%mod;return ret;
}int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);init();while(scanf("%d",&n)!=EOF){scanf("%s",str);len=strlen(str);bool flag=true;if(n%2==1) flag=false;int left=0,right=0;for(int i=0;i<len&&flag;i++){if(str[i]=='(') left++;else if(str[i]==')') right++;if(left>=right) continue;else flag=false;}if(flag==false) { puts("0"); continue; }int a=n/2-left; /// remain leftint b=n/2-right; /// remain rightif(b>a) swap(a,b);LL ans = (COMB(a+b,b)-COMB(a+b,b-1)+mod)%mod;cout<<ans<<endl;}return 0;
}

HDOJ 5184 Brackets 卡特兰数扩展相关推荐

  1. 【组合数学】 HDOJ 5184 Brackets

    附上BC的官方题解: 当n为奇数的时候答案是0. 先判断字符串的前面是否符合括号匹配,即对于任何前缀左括号个数>=右括号个数. 设左括号个数为a右括号个数为b, m=n/2,问题可以转化为在平面 ...

  2. 数论二(hdoj 卡特兰数)

    卡特兰数: 1 通项公式:h(n)=C(n,2n)/(n+1)=(2n)!/((n!)*(n+1)!) 2递推公式:h(n)=((4*n-2)/(n+1))*h(n-1); h(n)=h(0)*h(n ...

  3. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  4. zcmu-1934(卡特兰数大数取模(逆元))

    1934: ly的二叉树 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 42  Solved: 9 [Submit][Status][Web Boar ...

  5. 牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】

    链接:https://www.nowcoder.com/acm/contest/85/I 题目描述 あなたの蛙が帰っています!  蛙蛙完成了一趟旅行,回家啦!但它还是没有去它心中非常想去的几个地方.总 ...

  6. 【算法专题】卡特兰数

    卡特兰数 1. 概述 卡特兰数:首先这个一个数,很多问题的结果都是卡特兰数,比如2016年全国三卷数学选择题压轴题让求解的就是卡特兰数,问题如下: 首先是结论:卡特兰数为: C 2 n n n + 1 ...

  7. 从《编程之美》买票找零问题说起,娓娓道来卡特兰数——兼爬坑指南

    转自:从<编程之美>买票找零问题说起,娓娓道来卡特兰数--兼爬坑指南 引子: 大约两个月前,我在练习一些招聘的笔试题中,有一道和卡特兰数相关.那时还没来得及开始仔细看<编程之美> ...

  8. 算法基础 - 数论 | 组合数学 卡特兰数(Catalan number)定义、证明及例题

    写在前面:卡特兰数这东西感觉挺常用的,并且公式很简单,那就花一下午总结一下,学点皮毛吧(反正遇到我还是不会 ) [PDF] 大三上组合数学课堂讲义 文章目录 卡特兰数定义 卡特兰数的性质 卡特兰数证明 ...

  9. Catalan数——卡特兰数

    今天阿里淘宝笔试中碰到两道组合数学题,感觉非常亲切,但是笔试中失踪推导不出来 后来查了下,原来是Catalan数.悲剧啊,现在整理一下 Catalan数--卡特兰数] 一.Catalan数的定义令h( ...

最新文章

  1. 深入理解计算机系统(1.3)---金字塔形的存储设备、操作系统的抽象概念
  2. 怎么允许键盘鼠标唤醒计算机,Windows10如何禁止键盘鼠标唤醒电脑?
  3. EOS 智能合约源代码解读 (7)合约开发示例
  4. scrapy爬虫系列之三--爬取图片保存到本地
  5. 一步步优化JVM六:优化吞吐量[转]
  6. ChannelSplitterNode
  7. DeFi收益聚合协议Pickle Finance与APY Vision达成合作
  8. 学习笔记--对最近学习的总结
  9. Django + MySQL + Redis 编写简单的高校求职招聘管理系统 in English(毕设+源码)
  10. 荧光微球激发光谱图/橙色荧光微球/绿色荧光微球/氨基红色荧光微球/氨基橙色荧光微球
  11. Mysqldump原理及使用方法
  12. DIY单片机串口打印函数print
  13. 突发!字节跳动AI Lab总监李磊离职!加盟美国高校,曾为百度少帅科学家,交大ACM班成员...
  14. [COGS 2583]南极科考旅行
  15. 电子科技20秋C语言在线作业3,【奥鹏】[电子科技大学]20秋《C语言》在线作业3...
  16. 腾讯汤道生:微信乘车码已全量上线昆明地铁
  17. 为2017年出生的孩子免费取名字,孩子起名要注意的问题
  18. 阮一峰老师的JavaScript标准参考教程:函数和ES6函数的拓展
  19. Android 谷歌相册-云照片-分享到应用 方法
  20. Android 动态壁纸引擎试刀学习(初识安卓引擎)

热门文章

  1. ios笔记-单例文字代码详解
  2. oracle decode函数
  3. 机器学习——Day 1 数据预处理
  4. springmvc 配置aop
  5. git-osc自己定义控件之:CircleImageView
  6. EclipseIDEA使用经验
  7. oracle学习笔记(4)
  8. 理解 .NET 2015
  9. Quartz中时间表达式的设置-----corn表达式
  10. 关于QQ的几点改进建议