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. 总结const

    int b; const int  *a=&b; int const * a=&b; int * const a =&b; const int *const a=&b; ...

  2. lincode-58-四数之和

    58-四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 注意事项 四元组(a, b, c, d)中,需要满足a <= b &l ...

  3. lintcode-47-主元素 II

    47-主元素 II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 注意事项 数组中只有唯一的主元素 样例 给出数组[1,2,1,2,1,3,3] 返回 1 挑战 ...

  4. MvcMusicStore学习中常出现的一个BUG

    BUG描述:var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre); 前 ...

  5. jquery在页面加载完成后再append的元素事件无效问题

    最近遇到一个问题,jquery在页面加载完成后再append的元素,append元素上有onclick事件,但是在append的元素上怎么点击都不会触发onclick事件.就如: <ul cla ...

  6. [转] const int *a与int *const a,const int *const a的区别

    http://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰的位置在 ...

  7. exit和die的区别

    网上搜索die与exit两个函数的区别,大部分的"标准答案"都是说die是退出并释放内存,exit是退出但不释放内存. 这个解释显然是错的,PHP手册中已经说过"die ...

  8. 12.25模拟赛T2

    https://www.luogu.org/blog/a23333/post-xing-xuan-mu-ni-sai-path-ji-wang-zui-duan-lu 如果设f[i]表示从i到n的期望 ...

  9. git使用笔记(七)版本回退和撤销

    By francis_hao    Nov 21,2016 从版本库初始化开始,每一步的撤销操作 添加第一个文件 在空的版本库中创建了一个文件并git add到了缓存区,这时候怎么撤销呢? 撤销单个文 ...

  10. 正确答案 [Hash/枚举]

    正确答案 题目描述 小H与小Y刚刚参加完UOIP外卡组的初赛,就迫不及待的跑出考场对答案. "吔,我的答案和你都不一样!",小Y说道,"我们去找神犇们问答案吧" ...