【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两种 ...
随机推荐
- Java用DOM操作xml
JAXP DOM方式解析XML文档实例增删改查package jiexi; import javax.xml.parsers.DocumentBuilder; import javax.xml.par ...
- (转)C#中的Dictionary字典类介绍
关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...
- html调用applet
1.相同目录下 <applet code="*.class" width=250 height=50> </applet> 指定applet类名称,appl ...
- 深入理解Javascript之this关键字
深入理解Javascript之this关键字 作者: Laruence( ) 本文地址: http://www.laruence.com/2009/09/08/1076.html 转载请注明出处 ...
- js使用正则表达式去空格
写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...
- 在终端里使用 Solarized 配色方案
在终端里使用 Solarized 配色方案 参考: 1.在 Mac OS X 终端里使用 Solarized 配色方案 2.solarized
- OpenGL中的深度、深度缓存、深度测试及保存成图片
1.深度 所谓深度,就是在openGL坐标系中,像素点Z坐标距离摄像机的距离.摄像机可能放在坐标系的任何位置,那么,就不能简单的说Z数值越大或越小,就是越靠近摄像机. 2.深度缓冲区 深度缓冲区原理就 ...
- CodeFirst 的编程方式
第一步:创建控制台项目第二步:添加新建项目→Ado.Net空实体模型第三步:添加实体:Customer,添加几个必要的测试字段第四步:添加实体之间的联系第五步:根据模型生成数据库脚本,并执行sql脚本 ...
- mkpasswd
-l # (length of password, default = 7) 指定密码的长度,默认是7位数 -d # (min # of dig ...
- 《sort命令的k选项大讨论》-linux命令五分钟系列之二十七
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...