【HDU 5184】 Brackets (卡特兰数)
Brackets
Problem DescriptionWe 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 sequenceFor 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.
InputMulti 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.OutputFor each case,output answer % 1000000007 in a single line.Sample Input4()4(6()

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 1000010
#define Mod 1000000007
#define LL long long char s[Maxn];
int p[Maxn]; void init()
{
p[]=;
for(int i=;i<=Maxn-;i++)
{
LL x=(LL)p[i-],y=(LL)i,z;
z=(x*y)%Mod;
p[i]=(int)z;
}
} LL qpow(int x,int b)
{
if(x==) return ;
LL xx=x,ans=;
while(b)
{
if(b&) ans=(ans*xx)%Mod;
xx=(xx*xx)%Mod;
b>>=;
}
return ans;
} int get_c(int n,int m)
{
LL ans=p[m];
ans=(ans*qpow(p[n],Mod-))%Mod;
ans=(ans*qpow(p[m-n],Mod-))%Mod;
return (int)ans;
} int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF)
{
int m,sl=;
scanf("%s",s+);
int l=strlen(s+),now=;
bool ok=;
for(int i=;i<=l;i++)
{
if(s[i]=='(') now++,sl++;
else now--;
if(now<) ok=;
}
if(n%!=||l>n||!ok||sl*>n||(l-sl)*>n) {printf("0\n");continue;}
m=n/-sl;
if(sl==n/||l==n) {printf("1\n");continue;}
printf("%d\n",(get_c(m,*m+now)+Mod-get_c(m-,*m+now))%Mod);
}
return ;
}
[HDU 5184]
2016-09-20 19:53:38
【HDU 5184】 Brackets (卡特兰数)的更多相关文章
- HDOJ 5184 Brackets 卡特兰数扩展
既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1) /// 折纸法证明卡特兰数: h ...
- hdu 5184(数学-卡特兰数)
Brackets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5184 类卡特兰数+逆元
BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...
- hdu 5673 Robot 卡特兰数+逆元
Robot Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- hdu 4828 Grids 卡特兰数+逆元
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Problem D ...
- hdu 4828 Grids(拓展欧几里得+卡特兰数)
题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...
- 【HDU 5370】 Tree Maker(卡特兰数+dp)
Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...
- HDU 4828 (卡特兰数+逆)
HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看 ...
- HDU 6084 寻找母串(卡特兰数)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6084 [题目大意] 对于一个串S,当它同时满足如下条件时,它就是一个01偏串: 1.只由0和1两种 ...
随机推荐
- skynet是什么
云风的skynet,定义为一个游戏服务器框架,用c + lua基于Actor模型实现.代码极其精简,c部分的代码只有三千行左右. 整个skynet框架要解决的核心问题是:把一个消息 ...
- Linux常用系统调用
转载 http://www.ibm.com/developerworks/cn/linux/kernel/syscall/part1/appendix.html#icomments 按照惯例,这个列表 ...
- 在Windows下自动运行Modelsim
首先声明:该文章是在刘志伟老师的<Modelsim的Tcl命令>的基础上写的,希望我们能越来越自动化. 1.编写好源文件.包含asyn_fifo.v.fifomem.v.rptr_empt ...
- Yii框架页面运行流程
Yii框架页面运行流程 CComponent | CModel | CActiveRecord.CFormModel(所有模型的父类) | 表名.php(模型) | 入口文件------------- ...
- MySQL基本查询语句
创建一张表 create table user ( id ) not null, name ) not null, birthDate date not null, gender ) not null ...
- 20160331javaweb之JSP 标签技术
jsp的标签技术:在jsp页面中最好不要出现java代码,这时我们可以使用标签技术将java代码替换成标签来表示 1.jsp标签:sun原生提供的标签直接在jsp页面中就可以使用 <jsp:in ...
- android开发者博客二月-Marshmallow and User Data
又是一篇翻译,这篇快了很多,不过也花了快一个小时,可能熟悉一点.关于6.0权限的,让你做用户认为正确的事情. Marshmallow and UserData 2016,2,1 棉花糖和用户数据 由J ...
- 常用JS正则表达式收集
1.去掉字符串前后空格,不会修改原有字符串,返回新串.str.replace(/(^\s*)|(\s*$)/g,'');
- 【转】主从同步出现一下错误:Slave_IO_Running: Connecting
主从同步出现一下错误: Slave_IO_Running: ConnectingSlave_SQL_Running: Yes 解决方法: 导致lave_IO_Running 为connecting 的 ...
- 如何在WCF中集成unity
第一种是代码方式: 点击打开链接http://blogs.microsoft.co.il/gadib/2010/11/30/wcf-and-unity-20/ 还有一种方式可以扩展成配置文件,有时间再 ...