既求从点(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

Problem Description
We 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 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.
 
Input
Multi 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.
 
Output
For each case。output answer % 1000000007 in
a single line.
 
Sample Input
4
()
4
(
6
()
 
Sample Output
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;
}

最新文章

  1. QT常用资料
  2. java.sql.preparedstatement和java.sql.statement的区别
  3. php命令行运行出现错误Call to undefined function curl_init()
  4. 【译】Spark官方文档——编程指南
  5. POJ2251Dungeon Master
  6. HTTP 返回状态代码详解
  7. oracle学习笔记(1)-三级模式SCHEMA
  8. pwnable.kr random 之 write up
  9. 五一之起一台服务器玩玩-u盘安装centos
  10. 2018项目总结(vue+apicloud)
  11. [Solution] 893. Groups of Special-Equivalent Strings
  12. Go基础系列:互斥锁Mutex和读写锁RWMutex用法详述
  13. Pychram IDE链接MySQL下更新数据的问题总结
  14. Laravel自定义 封装便捷返回Json数据格式引用
  15. Linq去重 不用实现IEqualityComparer接口的方法超级简单
  16. VSS + Eclipse 管理源码
  17. js json处理 双引号
  18. Assert随笔
  19. unity开发android游戏
  20. C#中的序列化和反序列化是什么、有什么作用、使用方法详解

热门文章

  1. 理解CSS Clip属性及用法
  2. readline函数分析
  3. Asp.Net MVC5 格式化输出时间日期
  4. 重构oceanbase的一个函数
  5. IIS短文件名漏洞修补方法之一改注册表一个注意项
  6. codeforces D.Mashmokh and ACM
  7. hdu 5139 Formula
  8. QVariant(相当于是Java里面的Object,起到一个数据类型“擦除”的作用,可以使用Q_DECLARE_METATYPE进行注册)
  9. AOP小结
  10. 【Linux】鸟哥的Linux私房菜基础学习篇整理(二)