既求从点(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. python everything is object

    python面向对象非常彻底,即使过程式的代码风格,python在运作的时候也是面向对象的.everything is object. 差异 在面向对象的理念上,python和非常工程化的面向对象语言 ...

  2. 性能测试实践-linux

    需求:线上系统性能优化,查找服务器和线上系统瓶颈 根据线上经验数据及期望值定量 数据   up down 线上数据 50 500 测试数据 100 500~2000+ 测试数据 200 500~200 ...

  3. python split()黑魔法

    split()用法: #!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.spli ...

  4. Python核心编程2第六章课后练习

    6-1 字符串 .string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? #!/usr/bin/env python def contain(str1 ...

  5. linux学习笔记---一些有趣的命令

    一 在说链接之前我们哈需要说明一个东西,就是inode,一个文件的名字可以有多个,但是inode里的i-number却只有一个,(inode是一个数据结构,里面存放文件的各种属性,属主,属组,权限,大 ...

  6. crf 分词(待)

    http://blog.csdn.net/marising/article/details/5769653

  7. Struts2 Tomcat的配置

    1. 下载Struts2包,网站http://struts.apache.org/download.cgi#struts2315 2. 将struts-2.3.15-all.zip 包解压到本地 3. ...

  8. 【Java】Java XML 技术专题

    XML 基础教程 XML 和 Java 技术 Java XML文档模型 JAXP(Java API for XML Parsing) StAX(Streaming API for XML) XJ(XM ...

  9. requirejs 合并方案

    http://snandy.iteye.com/blog/1595464 http://www.cnblogs.com/snandy/archive/2012/03/05/2378105.html h ...

  10. Polyfills

    填充物..github上比较全的列表 IE总是有填不完的坑,在此总结一下,可能暂时都是一知半解,错了我会慢慢改的. 首先html5标签需要填充一些class,可以用html5.js处理,或者html5 ...