大意: 给定括号字符串, 求多少个子序列是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);
}

最新文章

  1. 20160805_CentOS6_键盘快捷键
  2. Windows7隐藏字体
  3. OBJ-C
  4. 2.SQL语言进阶
  5. C#有关虚方法要知道的知识点:
  6. java与OC比较
  7. nginx 防火墙、权限问题
  8. 深入探索.NET框架内部了解CLR如何创建运行时对象
  9. FB面经Prepare: Friends Recommendation
  10. Flask项目笔记
  11. centos 7 mariadb安装
  12. 1. 模拟Queue
  13. android app 流量统计
  14. HDU1575-Tr 【矩阵快速幂】(模板题)
  15. DeepNetwork---tensorflow实现
  16. Codeforces Round #539&amp;#542&amp;#543&amp;#545 (Div. 1) 简要题解
  17. 20155318 2016-2017-2 《Java程序设计》第六周学习总结
  18. poj 1868 Antiarithmetic? 推断序列中是否有等差子序列
  19. SLG手游Java服务器的设计与开发——数据管理
  20. OpenGL中的需要注意的细节问题

热门文章

  1. 在thinkphp里面执行原生的sql语句
  2. Oracle和sql server中复制表结构和表数据的sql语句
  3. ACM题目————区间覆盖问题
  4. SNMP学习笔记之Centos7配置SNMP服务
  5. centos下nginx安装与配置
  6. 20145313张雪纯exp7
  7. CMD运行后关闭CMD 的窗口命令
  8. C# 将文件转换为 Stream
  9. POJ 1845 Sumdiv(求因数和 + 逆元)题解
  10. Unity3D学习笔记(十):Physics类和射线