大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下:

  • It is not empty (that is n ≠ 0).
  • The length of the sequence is even.
  • First $\frac{n}{2}$ charactes of the sequence are equal to "(".
  • Last $\frac{n}{2}$ charactes of the sequence are equal to ")".

枚举最内侧的左括号位置, 由范德蒙德恒等式$\sum\limits_{i=0}^k\binom{n}{i}\binom{m}{k-i}=\binom{n+m}{k}$

可以得到贡献为$\sum\limits_{i=1}^\infty\binom{L-1}{i-1}\binom{R}{i}=\binom{L+R-1}{L}$.

#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, L[N], R[N];
ll fac[N];
char s[N]; int C(int n, int m) {
if (n<m) return 0;
return fac[n]*inv(fac[m])%P*inv(fac[n-m])%P;
} int main() {
fac[1]=1;
REP(i,2,N-1) fac[i]=fac[i-1]*i%P;
scanf("%s", s+1);
n = strlen(s+1);
REP(i,1,n) L[i]=L[i-1]+(s[i]=='(');
PER(i,1,n) R[i]=R[i+1]+(s[i]==')');
ll ans = 0;
REP(i,1,n) if (s[i]=='(') {
ans+=C(L[i]+R[i]-1,L[i]);
}
printf("%lld\n", ans%P);
}

Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)的更多相关文章

  1. Intercity Travelling CodeForces - 1009E (组合计数)

    大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1 ...

  2. Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)

    大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...

  3. Different Subsets For All Tuples CodeForces - 660E (组合计数)

    大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$ ...

  4. Singer House CodeForces - 830D (组合计数,dp)

    大意: 一个$k$层完全二叉树, 每个节点向它祖先连边, 就得到一个$k$房子, 求$k$房子的所有简单路径数. $DP$好题. 首先设$dp_{i,j}$表示$i$房子, 分出$j$条简单路径的方案 ...

  5. GukiZ and Binary Operations CodeForces - 551D (组合计数)

    大意: 给定$n,k,l,m$, 求有多少个长度为$n$, 元素全部严格小于$2^l$, 且满足 的序列. 刚开始想着暴力枚举当前or和上一个数二进制中$1$的分布, 但这样状态数是$O(64^3)$ ...

  6. Max History CodeForces - 938E (组合计数)

    You are given an array a of length n. We define fa the following way: Initially fa = 0, M = 1; for e ...

  7. CodeForces - 5C(思维+括号匹配)

    题意 https://vjudge.net/problem/CodeForces-5C 给出一个括号序列,求出最长合法子串和它的数量. 合法的定义:这个序列中左右括号匹配. 思路 这个题和普通的括号匹 ...

  8. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  9. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [分治FFT 组合计数 | 多项式求逆]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

随机推荐

  1. python-自定义异常,with用法

    抛出异常 #coding=utf-8 def  exceptionTest(num): if num<0: print "if num<0" raise Excepti ...

  2. MySQL数据库----事务处理

    事物处理  一. 什么是事务    一组sql语句批量执行,要么全部执行成功,要么全部执行失败 二.为什么出现这种技术 为什么要使用事务这个技术呢? 现在的很多软件都是多用户,多程序,多线程的,对同一 ...

  3. linux环境下安装tomcat6

    1)下载apache-tomcat-6.0.10.tar.gz 2)#tar -zxvf apache-tomcat-6.0.10.tar.gz ://解压 3)#cp -R apache-tomca ...

  4. P3868 [TJOI2009]猜数字

    [TJOI2009]猜数字 中国剩余定理 求解i=1 to n : x≡a[i] (mod b[i])的同余方程组 设 t= ∏i=1 to n b[i] 我们先求出 i=1 to n : x≡1 ( ...

  5. c++中的字符集与中文

    就非西欧字符而言,比如中国以及港澳台,在任何编程语言的开发中都不得不考虑字符集及其表示.在c++中,对于超过1个字节的字符,有两种方式可以表示: 1.多字节表示法:通常用于存储(空间效率考虑). 2. ...

  6. 04: python常用模块

    目录: 1.1 时间模块time() 与 datetime() 1.2 random()模块 1.3 os模块 1.4 sys模块 1.5 tarfile用于将文件夹归档成 .tar的文件 1.6 s ...

  7. 20135234mqy-——信息安全系统设计基础第十一周学习总结

    第八章 异常控制流 8.1异常 异常是异常控制流的一种形式,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常就是控制流中的突变,用来响应处理器状态中的某些变化. 8.1.1异常处理 异常号是到 ...

  8. 把json的字符串变为json对象

    如{"tag":"sendcode","data":{"phone":"18880488738"}} ...

  9. 如何使用python来对二维数组进行排序

    1.复合排序 直接用numpy的lexsort就可以 import numpy as np data = np.array([[1,2,3,4,5], [1,2,3,6,7], [2,3,4,5,7] ...

  10. Python跨平台打包

    对于pyinstaller,可以完成在windows,linux,和mac下的python脚本编译,生成exe,elf,.app文件: 1.使用方法: 在pyinstaller的官网上下载,一般是源码 ...