HDOJ 5184 Brackets 卡特兰数扩展
既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法...
有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1) ///
折纸法证明卡特兰数: http://blog.sina.com.cn/s/blog_6917f47301010cno.html
Brackets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 506 Accepted Submission(s): 120
● 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.
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.
a single line.
4 () 4 ( 6 ()
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 ()(()).
- /* ***********************************************
- Author :CKboss
- Created Time :2015年03月18日 星期三 20时10分21秒
- File Name :HDOJ5184.cpp
- ************************************************ */
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <cstdlib>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- typedef long long int LL;
- const int maxn=1001000;
- const LL mod=1000000007LL;
- int n,len;
- char str[maxn];
- LL inv[maxn];
- LL jc[maxn],jcv[maxn];
- void init()
- {
- inv[1]=1; jc[0]=1; jcv[0]=1;
- jc[1]=1; jcv[1]=1;
- for(int i=2;i<maxn;i++)
- {
- inv[i]=inv[mod%i]*(mod-mod/i)%mod;
- jc[i]=(jc[i-1]*i)%mod;
- jcv[i]=(jcv[i-1]*inv[i])%mod;
- }
- }
- LL COMB(LL n,LL m)
- {
- if(m<0||m>n) return 0LL;
- if(m==0||m==n) return 1LL;
- LL ret=((jc[n]*jcv[n-m])%mod*jcv[m])%mod;
- return ret;
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- init();
- while(scanf("%d",&n)!=EOF)
- {
- scanf("%s",str);
- len=strlen(str);
- bool flag=true;
- if(n%2==1) flag=false;
- int left=0,right=0;
- for(int i=0;i<len&&flag;i++)
- {
- if(str[i]=='(') left++;
- else if(str[i]==')') right++;
- if(left>=right) continue;
- else flag=false;
- }
- if(flag==false) { puts("0"); continue; }
- int a=n/2-left; /// remain left
- int b=n/2-right; /// remain right
- if(b>a) swap(a,b);
- LL ans = (COMB(a+b,b)-COMB(a+b,b-1)+mod)%mod;
- cout<<ans<<endl;
- }
- return 0;
- }
HDOJ 5184 Brackets 卡特兰数扩展的更多相关文章
- hdu 5184(数学-卡特兰数)
Brackets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- CodeForces - 1204E Natasha, Sasha and the Prefix Sums (组合数学,卡特兰数扩展)
题意:求n个1,m个-1组成的所有序列中,最大前缀之和. 首先引出这样一个问题:使用n个左括号和m个右括号,组成的合法的括号匹配(每个右括号都有对应的左括号和它匹配)的数目是多少? 1.当n=m时,显 ...
- hdu 5184 类卡特兰数+逆元
BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...
- HDUOJ---1133(卡特兰数扩展)Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...
- [SCOI2010]生成字符串 题解(卡特兰数的扩展)
[SCOI2010]生成字符串 Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数 ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- HDOJ 1023 Train Problem II 卡特兰数
火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. ...
- hdoj 4828 卡特兰数取模
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...
随机推荐
- asp.net尽量不在js里写<%%>
asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...
- SGU 200.Cracking RSA(高斯消元)
时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...
- Codeforces 551C GukiZ hates Boxes(二分)
Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...
- SQL Server的事务处理与高级查询
6.高级查询与脚本 6.1子查询 位于SELECT查询中的SELECT查询. 6.11 标量表达式 select id,val,val-(select avg(val) from tbltest) f ...
- PAT - 基础 - 龟兔赛跑
题目: 乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息.乌龟每分钟可以前进3米,兔子每分钟前进9米:兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超 ...
- springMVC整合memcached,以注解形式使用
睡不着,深夜写点博客.闲下来有一个月了,心里多少有点…… 在北京找工作一再受阻,这个时间点也不好找 再接再厉 之前没有用过memcached,没有什么实战经验,看了一些关于memcached的博客,写 ...
- mysql数据类型——整型INT(m)
1.整形分为四种 tinyint smallint mediumint int bigint 注意: 右侧的取值范围是在未加unsigned关键字的情况下,如果加了unsigned,则最大值翻倍,如t ...
- sphinx (coreseek)——2、区段查询实例
首先需要知道区段查询的定义: 索引系统需要通过主查询来获取全部的文档信息,一种简单的实现是将整个表的数据读入内存,但是这可能导致整个表被锁定并使得其他操作被阻止(例如:在MyISAM格式上的INSER ...
- width:100%缩小窗口时背景图片出现空白bug
页面容器(#wrap)与页面头部(#header )为100%宽度.而内容的容器(#page)为固定宽度960px.浏览窗口缩小而小于内容层宽度时会产生宽度理解上的差异.如下图所示窗口宽度大于内容层宽 ...
- odoo view field option, action flage 参数
options JSON object specifying configuration option for the field's widget (including default widget ...