Brackets

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

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 ()(()).

 
Source
卡特兰数经典模型:
 
题意:给出一个数字 n ,然后会有 n 个 '(' 和 ')' 组成一个字符串序列,这个字符串序列的 ( 和 ) 是相互匹配的,现在给出这个字符串序列的前几项,要你判断符合前缀为给出序列的字符串会有多少种??
题解:如果没有前缀条件就是裸的卡特兰数的模型.如果已经确定了前缀,当前缀满足的情况下,对于后面的串这个时候 '(' 的数量不会会大于等于 ')' 的数量,所以我们将其看成一个经典的买票模型.
n+m个人排队买票,并且满足,票价为50元,其中n个人各手持一张50元钞票,m个人各手持一张100元钞票,除此之外大家身上没有任何其他的钱币,并且初始时候售票窗口没有钱,问有多少种排队的情况数能够让大家都买到票。
这里的话将 ')' 看成50 的,'(' 看成100的,只要当前的 ')'比 '(' 多我们总是可以找到一个序列.所以这里还是一个卡特兰数模型。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const LL mod = ;
const int N = ;
char s[N];
LL f[N];
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
void init(){
f[] = f[] = ;
for(int i=;i<N;i++){
f[i] = f[i-]*i%mod;
}
}
LL C(LL n,LL m){
LL a = f[m]*f[n-m]%mod;
LL inv = pow_mod(a,mod-);
return f[n]*inv%mod;
}
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",&s);
if(n%==){
printf("0\n");
continue;
}
int len = strlen(s);
int l=,r=;
bool flag = true;
for(int i=;i<len;i++){ ///已经加入的左括号必须不小于右括号
if(s[i]=='(') l++;
if(s[i]==')') r++;
if(l<r) {
flag = false;
break;
}
}
if(!flag||l<r){
printf("0\n");
continue;
}
int m= n/;
l = m-l,r = m-r;
if(l<||r<){ ///防止这种情况 4 ((()
printf("0\n");
continue;
}
printf("%lld\n",(C(l+r,r)-C(l+r,r+)+mod)%mod);
}
return ;
}

hdu 5184(数学-卡特兰数)的更多相关文章

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

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

  2. hdu 5673 Robot 卡特兰数+逆元

    Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  3. hdu 4828 Grids 卡特兰数+逆元

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

  4. HDOJ 5184 Brackets 卡特兰数扩展

    既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  /// 折纸法证明卡特兰数: h ...

  5. hdu2067 小兔的棋盘 DP/数学/卡特兰数

    棋盘的一角走到另一角并且不越过对角线,卡特兰数,数据量小,可以当做dp求路径数 #include<stdio.h> ][]; int main() { ; ) { int i,j; lon ...

  6. 【HDU 5184】 Brackets (卡特兰数)

    Brackets Problem Description We give the following inductive definition of a “regular brackets” sequ ...

  7. HDU 1023(卡特兰数 数学)

    题意是求一列连续升序的数经过一个栈之后能变成的不同顺序的数目. 开始时依然摸不着头脑,借鉴了别人的博客之后,才知道这是卡特兰数,卡特兰数的计算公式是:a( n )  =  ( ( 4*n-2 ) / ...

  8. hdu 4828 Grids(拓展欧几里得+卡特兰数)

    题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...

  9. 【HDU 5370】 Tree Maker(卡特兰数+dp)

    Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...

随机推荐

  1. C语言循环结构作业总结

    循环作业总结 1.1 基本要求 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 博客作业不规范,没有Markdown语法 - 扣分 泛泛而谈(最多七分) 1.2 ...

  2. AtCoder Grand Contest 028 B - Removing Blocks 解题报告

    B - Removing Blocks Time limit : 2sec / Memory limit : 1024MB Score : 600 points ## Problem Statemen ...

  3. 【NOIP2017 D1 T1 小凯的疑惑】

    题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...

  4. HTML5 Canvas圣诞树

    又逢圣诞了,为了让小站NowaMagic有点节日气氛,这里也弄一棵圣诞树放放-大家可以先看下效果. 效果演示 <canvas id="c"></canvas> ...

  5. Win7命令mklink的使用

    C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了AppData,Chrome占了1G多的空间. 当时安装Chrome浏览器时因为不能指定安装目录,所以Chrome ...

  6. 原生toolbar基本使用教程

    1.先写布局文件 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:title=" ...

  7. arraylist和array的不同之处

    https://www.cnblogs.com/wangbin2188/p/6524200.html

  8. 封装getByClass(JS获取class的方法封装为一个函数)

    获取方法一(普通版) 获取单一的class: function getByClass(oParent, sClass) {//两个形参,第一个对象oParent 第二个样式名class var aEl ...

  9. 自旋锁、排队自旋锁、MCS锁、CLH锁

    转载自:http://coderbee.net/index.php/concurrent/20131115/577 自旋锁(Spin lock) 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他 ...

  10. 布局之BFC

    BFC 什么是BFC,在哪里需要用到BFC,BFC有什么规则?生成BFC有什么条件?这几个问题,我将为大家一一解释,下面我们进入正题. BFC(Block formatting context)直译为 ...