既求从点(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. Codeforces 441D Valera and Swaps(置换群)

    题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...

  2. python3 读取大文件分解成若干小文件

    有个数据实在太大了,有1.7G,打开慢,改文件也慢,我们将其分解成若干个中等文件 #!/usr/bin/env python3 # -*- coding: utf-8 -*-   f = open(& ...

  3. truncate 命令删除恢复

    truncate命令可以一次性删除当前表中所有记录并且不留任何日志,同时这个表的ID就自动初化从1开始,今天我就来给大家尝试一个利用truncate清除记录之后恢复过程. 实际线上的场景比较复杂,当时 ...

  4. python 时间戳

    import timeprint time.time()输出的结果是(单位:s):1395421406.39 x = time.localtime(x) x = time.strftime('%Y-% ...

  5. java项目导出jar文件时指定main方法的类

    需要先运行一下main函数,eclipse的Export-->Runnable JAR File ---> 下的Launch configuration下拉列表才会有记录.如果想要删除下拉 ...

  6. Keil c51现No Browse information available

    keil c51 不能使用:Go to Definition of....的解决方法 最近使用keil c51 开发usb固件,当向vc一样使用Go to Definition of....时,出现警 ...

  7. mapreduce (七) 几个实例

    http://hi.baidu.com/hzd2712/item/d2465ae65270ab3e4cdcaf55 MapReduce几个典型的例子 在Google的<MapReduce: Si ...

  8. 【Java】如何访问服务器

    HTTP协议---------->GET.POST.XMLHttpRequest TCP/IP协议 SOAP协议---------->Web Service Server的作用是处理HTT ...

  9. SHELL要发送HTML这类邮件的话,还得靠msmtp 和 mutt

    参考蛮多的.. http://storysky.blog.51cto.com/628458/293005 http://www.wilf.cn/post/centos-mutt-msmtp-setup ...

  10. FileUtils

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...