hdu 5184(数学-卡特兰数)
Brackets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 659 Accepted Submission(s): 170
● 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.
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.
()
4
(
6
()
2
2
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 ()(()).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const LL mod = ;
const int N = ;
char s[N];
LL f[N];
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
void init(){
f[] = f[] = ;
for(int i=;i<N;i++){
f[i] = f[i-]*i%mod;
}
}
LL C(LL n,LL m){
LL a = f[m]*f[n-m]%mod;
LL inv = pow_mod(a,mod-);
return f[n]*inv%mod;
}
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",&s);
if(n%==){
printf("0\n");
continue;
}
int len = strlen(s);
int l=,r=;
bool flag = true;
for(int i=;i<len;i++){ ///已经加入的左括号必须不小于右括号
if(s[i]=='(') l++;
if(s[i]==')') r++;
if(l<r) {
flag = false;
break;
}
}
if(!flag||l<r){
printf("0\n");
continue;
}
int m= n/;
l = m-l,r = m-r;
if(l<||r<){ ///防止这种情况 4 ((()
printf("0\n");
continue;
}
printf("%lld\n",(C(l+r,r)-C(l+r,r+)+mod)%mod);
}
return ;
}
hdu 5184(数学-卡特兰数)的更多相关文章
- 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 ...
- HDOJ 5184 Brackets 卡特兰数扩展
既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1) /// 折纸法证明卡特兰数: h ...
- hdu2067 小兔的棋盘 DP/数学/卡特兰数
棋盘的一角走到另一角并且不越过对角线,卡特兰数,数据量小,可以当做dp求路径数 #include<stdio.h> ][]; int main() { ; ) { int i,j; lon ...
- 【HDU 5184】 Brackets (卡特兰数)
Brackets Problem Description We give the following inductive definition of a “regular brackets” sequ ...
- HDU 1023(卡特兰数 数学)
题意是求一列连续升序的数经过一个栈之后能变成的不同顺序的数目. 开始时依然摸不着头脑,借鉴了别人的博客之后,才知道这是卡特兰数,卡特兰数的计算公式是:a( n ) = ( ( 4*n-2 ) / ...
- 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 ...
随机推荐
- 关于debian配置的问题汇总
debian的apache多域名配置: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-ho ...
- PAT 甲级 1042 Shuffling Machine
https://pintia.cn/problem-sets/994805342720868352/problems/994805442671132672 Shuffling is a procedu ...
- Linux杂技
挂载光盘 mkdir /mnt/cdrom #建立挂载点 mount /dev/cdrom /mnt/cdrom/ #挂载光盘 更换YUM源: cd /etc/yum.repos.d/ 使网络yum源 ...
- Hibernate关联映射之_一对一
数据库模型 一般对一对一的关系而言,会存在一个主从关系.如 人 与 身份证,就是一个一对一关系, 人 是主,身份证 是从 Person PK:id name age Id_Card PK.FK:id ...
- JavaScript归并方法reduce()和reduceRight()
ECMAScript 5还新增了两个归并数组的方法:reduce()和reduceRight().这两个方法都会迭代数组的所有项,然后构建一个最终返回的值.其中,reduce()方法从数组的第一项开始 ...
- NOIP临考经验【转】
NOIP临考经验 1.提前15分钟入场,此时静坐调整心态,适当的深呼吸 2.打开编辑器并调整为自己喜欢的界面 3.熟悉文件目录,写好准确无误的代码模板 4.压缩包或许还不能解压,但是文件名已经可以知道 ...
- js生成图片
var image = new Image(); var c = document.getElementById("myCanvas"); var ctx = c.getConte ...
- 一些比较高效的CSS写法建议
当浏览器解析html的时候,它构造了一个文档树来展现所有被显示的元素. 它在特定的样式表中去匹配元素,根据标准的css的层叠,继承和顺序规则, 在mozilla的实现中(可能其他的也是这样),对于每一 ...
- SpringMVC学习 -- 使用 POJO 对象绑定请求参数值
Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 , 自动为该对象填充属性值 , 支持级联属性.如:address.province. package com.itdoc.spri ...
- JS知识总结
1.javascript继承机制 原型继承,访问对象属性时,如果对象内部有就返回,找不到就会从对象原型指向的对象原型中查找,一层一层的查找,直到最顶层的对象原型还找不到,就返回undefined. 2 ...