传送门

题意

给出一个只包含'(',')'的字符序列,询问有多少个\(RSBS\)

分析

首先需要知道一个公式

\[\sum_{i=0}^{min(x,y)}C_x^i*C_y^i=C_{x+y}^x
\]

接下来我们观察第i个'(',假设它左边有x个'(',右边有y个')',那么包含它的RSBS有多少个?答案是\(C_{x+y-1}^x\),因为它已经“消耗了”一个')',所以左边的'('只能与右边的y-1个')'匹配。最后扫一遍即可。

代码

#include<bits/stdc++.h>

const int N = 400010;
const int moder = 1e9 + 7; int fac[N], inv[N];
char s[N]; int power_mod(int a, int index){
int ret = 1;
while (index){
if (index & 1){
ret = 1ll * ret * a % moder;
}
a = 1ll * a * a % moder;
index >>= 1;
}
return ret;
} int calc(int x, int y){
return 1ll * fac[x + y - 1] * inv[y - 1] % moder * inv[x] % moder;
} void init()
{
fac[0] = 1;
for (int i = 1; i < N; ++ i){
fac[i] = 1ll * fac[i - 1] * i % moder;
}
inv[N - 1] = power_mod(fac[N - 1], moder - 2);
for (int i = N - 2; i >= 0; -- i){
inv[i] = 1ll * inv[i + 1] * (i + 1) % moder;
}
} int main(){
init();
scanf("%s", s);
int cnt2 = 0, len = strlen(s);
for (int i = 0; i < len; ++ i){
if (s[i] == ')'){
++ cnt2;
}
}
int cnt1 = 0, ans = 0;
for (int i = 0; i < len; ++ i){
if (s[i] == ')'){
-- cnt2;
continue;
}
++ cnt1;
ans = (ans + calc(cnt1, cnt2)) % moder;
}
return printf("%d\n", ans), 0;
}

Codeforces785D - Anton and School - 2的更多相关文章

  1. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

  2. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  3. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  4. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  5. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  6. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  7. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  8. Codeforces Round #379 (Div. 2) A. Anton and Danik 水题

    A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟

    题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...

随机推荐

  1. Android操作系统架构

    Android操作系统架构   Android操作系统整体应用架构 Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高 ...

  2. msp430入门编程35

    msp430中C语言的可移植--规划软件层次

  3. Flex使用cookie保存登状态

    flex系统要实现普通html系统刷新页面保留登录信息,一个方法是借用js来操作cookie来实现,下边是一个实现的例子: 第一步:在主index.html里边加上两个js方法 //获取cookie内 ...

  4. Spring的JDBC示例

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/jdbc-framework-overview/spring-jdbc-example.html: ...

  5. linux中的线程局部存储(TLS)

    http://blog.csdn.net/cywosp/article/details/26469435

  6. 设计模式 之代理(Proxy)模式

    为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事.找别人去做,这就是代理.所以,当你写代码的时候.你想保持类的简单性.重用性.你就能够把事件尽量都交给其他类去做.自己仅仅管做好自己的事.也就 ...

  7. EXCEPT(差集)集合运算

    在集合论中,集合A与B的差集(A-B)是由属于集合A,但不属于集合B的元素组成的集合.可以认为两个集合的差A-B就是从A中减去B中也属于A的元素. 在T-SQL中,集合之差是用EXCEPT集合运算实现 ...

  8. 10.2.0.1.1 grid control的启动和关闭

    一.Stopping Grid Control and All Its Components 1.停止OMS服务 [oracle@ocm2 oms10g]$ cd /u01/app/oracle/Or ...

  9. Nginx系列三 内存池的设计

    Nginx的高性能的是用非常多细节来保证,epoll下的多路io异步通知.阶段细分化的异步事件驱动,那么在内存管理这一块也是用了非常大心血.上一篇我们讲到了slab分配器,我们能够能够看到那是对共享内 ...

  10. Kemans算法及其Python 实现

    算法优缺点: 优点:容易实现缺点:可能收敛到局部最小值,在大规模数据集上收敛较慢使用数据类型:数值型数据 算法思想 k-means算法实际上就是通过计算不同样本间的距离来判断他们的相近关系的,相近的就 ...