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. direct.h头文件(对目录操作)

    chdir()改变当前目录的函数原形:int chdir(const char *path)功能:把由path指定的目录改为当前目录.path参数中可以指定驱动器号,如“a:\\ddd”, 但只是改变 ...

  2. 浅谈c语言和c++中struct的区别

    今天做二叉树的时候,发现利用结构体有点乱,不知道怎么回事,我之前知道c语言中声明一个结构体变量时需要通过 struct 结构体名 变量名,而在c++中,可以不要struct,由于可以利用typedef ...

  3. oracle分区技术提高查询效率

    概述: 当表中的数据量不断增大,查询数据的速度就会变慢,应用程序的性能就会下降,这时就应该考虑对表进行分区.表进行分区后,逻辑上表仍然是一张完整的表,只是将表中的数据在物理上存放到多个表空间(物理文件 ...

  4. 批处理之SET命令

    除了 下面分别介绍: 表示第二个字符到倒数第三个字符的值

  5. [UOJ #51]【UR #4】元旦三侠的游戏

    题目大意:给$n$,一个游戏,给$a,b$,两个人,每人每次可以把$a$或$b$加一,要求$a^b\leqslant n$,无法操作人输.有$m$次询问,每次给你$a,b$,问先手可否必胜 题解:令$ ...

  6. CSS网页宽度怎么定比较合适

    设计网页的时候,确定宽度是一件很苦恼的事.以nowamagic.net为例,根据Google Analytics的统计,半年多以来,访问者的屏幕分辨率一共有81种.最小的分辨率是122x160,这应该 ...

  7. 用npm安装express时报proxy的错误的解决方法

    首先要说明一点:当使用npm install <module-name>时安装组件时,安装的目录是cmd的目录+node_modules+组件名 例子如下:假如你现在安装express这个 ...

  8. Windows Server 2008 R2 Upgrade Paths

    https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd ...

  9. Windows Time Client

    Timezone: UTC Coordinated Universal Time ====Perform by Local / administrator must,configure Time se ...

  10. ios上传图片显示方向错误问题

    IOS 上传图片方向显示错误问题 问题描述 在使用苹果手机上传图片的时候,发现传完的图片显示出来方向是错误的,竖着的图片会变成横着显示(少部分安卓手机也存在这个问题) 产生原因 ios 相机加入了方向 ...