【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()
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 using namespace std; 9 #define Maxn 1000010 10 #define Mod 1000000007 11 #define LL long long 12 13 char s[Maxn]; 14 int p[Maxn]; 15 16 void init() 17 { 18 p[0]=1; 19 for(int i=1;i<=Maxn-10;i++) 20 { 21 LL x=(LL)p[i-1],y=(LL)i,z; 22 z=(x*y)%Mod; 23 p[i]=(int)z; 24 } 25 } 26 27 LL qpow(int x,int b) 28 { 29 if(x==0) return 1; 30 LL xx=x,ans=1; 31 while(b) 32 { 33 if(b&1) ans=(ans*xx)%Mod; 34 xx=(xx*xx)%Mod; 35 b>>=1; 36 } 37 return ans; 38 } 39 40 int get_c(int n,int m) 41 { 42 LL ans=p[m]; 43 ans=(ans*qpow(p[n],Mod-2))%Mod; 44 ans=(ans*qpow(p[m-n],Mod-2))%Mod; 45 return (int)ans; 46 } 47 48 int main() 49 { 50 init(); 51 int n; 52 while(scanf("%d",&n)!=EOF) 53 { 54 int m,sl=0; 55 scanf("%s",s+1); 56 int l=strlen(s+1),now=0; 57 bool ok=1; 58 for(int i=1;i<=l;i++) 59 { 60 if(s[i]=='(') now++,sl++; 61 else now--; 62 if(now<0) ok=0; 63 } 64 if(n%2!=0||l>n||!ok||sl*2>n||(l-sl)*2>n) {printf("0\n");continue;} 65 m=n/2-sl; 66 if(sl==n/2||l==n) {printf("1\n");continue;} 67 printf("%d\n",(get_c(m,2*m+now)+Mod-get_c(m-1,2*m+now))%Mod); 68 } 69 return 0; 70 }
[HDU 5184]
2016-09-20 19:53:38
转载于:https://www.cnblogs.com/Konjakmoyu/p/5890149.html
【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 5673 Robot 卡特兰数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题目描述: 一个人从原点开始向右走, 要求N秒后回到原点, 且过程中不能到负半轴, 人有两种操 ...
- hdu 1023 大数 卡特兰数
卡特兰数 JAVA大数 import java.util.*; import java.math.*; public class Main {public static void main(Strin ...
- 卡特兰数 相关问题 hdu 5184 Brackets
题解: 当n为奇数的时候答案是0. 先判断字符串的前面是否符合括号匹配,即对于任何前缀左括号个数>=右括号个数. 设左括号个数为a右括号个数为b, m=n/2,问题可以转化为在平面中从座标(a, ...
- HDU 3240 Counting Binary Trees 数论-卡特兰数
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3240 卡特兰数递推公式h(i)=h(i-1)*(4*i-2)/(i+1) 如果直接算每一步,然后mo ...
- 【HDU - 1134 】Game of Connections(JAVA大数加法,卡特兰数)
题干: This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - ...
- hdu 1134 卡特兰数(大数模板)
卡特兰数 递推公式: C(n)=C(2n,n)/(n+1) 即用数组表示为c[i]=c[i-1]*(4*i-2)/(i+1); 一般形式 直接 表达 c[1]=1; for(i=2;i<40; ...
- HDU 3723 Delta Wave(卡特兰数+大数)
题意:从坐标(0, 0)到(n, 0)的折线,这条折线每向右延伸一个单位长度,高度要么不变,要么+1,要么-1,(不能到y=0以下)已知n,求这种折线种数 思路:我们知道上升和下降的次数要一样,而这就 ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
最新文章
- 2-2. 控制对文件的访问
- 【转】python装饰器
- python 内置模块 subprocess
- 达梦工作笔记-使用达梦客户端创建用户并授予权限
- OpenCV 编译 - Unable to locate package libjasper-dev
- pandas最常用的几个功能,随时翻阅就差不多弄懂了pandas的75%左右
- JAVA代码实现hive连接mysql_Java采用JDBC的方式连接Hive(SparkSQL)
- A. 解决运行php文件出现乱码的问题
- json ajax查询,jQuery AJAX和JSON性能查询
- android开发之路05
- 图形库LVGL v8.2版本移植
- 帝国CMS Table '***.phome_ecms_news_data_' doesn't exist
- 互联网金融VS区块链金融
- unity透明通道加颜色_半透明物体的描边和投影
- matlab等于怎么表示,MATLAB里的不等于用什么表示?
- 阿里云ECS服务器被DDoS无解攻击,我改怎么办
- 电商直播增加人气,留人技巧有哪些
- 透明网关与透明防火墙
- 二零一九第二天 文/一个会写诗的程序员
- 【鬼畜】UVA - 401每日一题·猛男就是要暴力打表