既求从点(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 left
int b=n/2-right; /// remain right if(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. hdu 5184(数学-卡特兰数)

    Brackets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. CodeForces - 1204E Natasha, Sasha and the Prefix Sums (组合数学,卡特兰数扩展)

    题意:求n个1,m个-1组成的所有序列中,最大前缀之和. 首先引出这样一个问题:使用n个左括号和m个右括号,组成的合法的括号匹配(每个右括号都有对应的左括号和它匹配)的数目是多少? 1.当n=m时,显 ...

  3. hdu 5184 类卡特兰数+逆元

    BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...

  4. HDUOJ---1133(卡特兰数扩展)Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

    题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...

  6. [SCOI2010]生成字符串 题解(卡特兰数的扩展)

    [SCOI2010]生成字符串 Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数 ...

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

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

  8. HDOJ 1023 Train Problem II 卡特兰数

    火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. ...

  9. hdoj 4828 卡特兰数取模

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

随机推荐

  1. 将fastjson元素转化为String[]

    在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...

  2. php5.6安装

    # yum --enablerepo=remi,remi-php56 install php-fpm php-common php-mysql php-opcache php-pear php-gd ...

  3. JQuery 绑定回车事件 兼容ie8,ie9

    $("#form-search").find('#search-query').bind('keypress', function(e) { var keycode; if(win ...

  4. YII框架的部署 通过YII脚手架程序创建应用程序系统

    1,把YII框架里面的framework复制粘贴到nginx目录下 2,创建一个商城系统: 1)修改环境变量 制定php.exe的目录 2)C:\Users\Administrator>cd C ...

  5. delphi 2010 动态链接库DLL断点调试

    DELPHI 2010 动态链接库DLL断点调试 马根峰 (广东联合电子服务股份有限公司,广州 510300) 摘要:本文详细介绍了Delphi 2010中的动态链接库DLL断点调试技术 关键词:DE ...

  6. STM32学习笔记——定时器中断(向原子哥学习)

    定时器中断 STM32 的定时器功能十分强大,有 TIME1 和 TIME8 等高级定时器,也有 TIME2~TIME5 等通用定时器,还有 TIME6 和TIME7 等基本定时器.在本章中,我们将利 ...

  7. DotNet 资源

    DotNet 资源 目录 API 应用框架(Application Frameworks) 应用模板(Application Templates) 人工智能(Artificial Intelligen ...

  8. Solr4.8.0源码分析(24)之SolrCloud的Recovery策略(五)

    Solr4.8.0源码分析(24)之SolrCloud的Recovery策略(五) 题记:关于SolrCloud的Recovery策略已经写了四篇了,这篇应该是系统介绍Recovery策略的最后一篇了 ...

  9. 全是干货---Linux 高可用(HA)集群基本概念详解

    http://www.linuxidc.com/Linux/2013-08/88522.htm 高可用集群的衡量标准    HA(High Available), 高可用性群集是通过系统的可靠性(re ...

  10. C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)

    原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...