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
2HintFor 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 ...
随机推荐
- cuda中时间用法
转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...
- 【BZOJ2809】【splay启发式合并】dispatching
Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...
- css3基础教程十六变形与动画animation
前面我们讲过的变形与动画一般都是通过鼠标的单击.获得焦点,被点击或对元素进行一定改变后以后触发效果的,那么有没有像Flash一样自动播放的动画效果呢?答案当然是肯定的,这就是我们今天要讲到的anima ...
- iOS 视频播放横屏,隐藏状态栏
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] init]; ...
- applicationContext.xml详解(转)
转自:http://blog.csdn.net/heng_ji/article/details/7022171,写的很好,省得以后找,放此处 想必用过Spring的程序员们都有这样的感觉,Spring ...
- U盘美化(更换U盘logo和页面背景软件)
U盘内新建txt文本后,输入 [autorun] ICON=ooopic_1459309050.ico 保存的文件名包括后缀更改为autorun.inf 必须为icon图标
- ThinkPHP框架下,给jq动态添加的标签添加点击事件移除标签
jq移除标签主要就是$("#要移除的id").remove();不再赘述,这里要提醒的是jq中动态添加标签后怎样添加点击事件.一般的jq添加点击事件是用这种方法$("#i ...
- access_token的获取方式
获取Access Token $appid = ""; $appsecret = ""; $url = "https://api.weixin.q ...
- 如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变?(自绘ComboBox) [转]
原文地址:http://bbs.csdn.net/topics/390135022 http://blog.csdn.net/scsdn/article/details/4363299 想使用winf ...
- Solr4.6从数据库导数据的步骤
http://blog.csdn.net/bruce128/article/details/17796705 Solr4.6有从数据库导数据的功能.导入步骤如下: 1.将下载下来的solr4.6的di ...