传送门

题意

给出一个只包含'(',')'的字符序列,询问有多少个\(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. secure上传图片

    1.rz -bye 2.upload as ascii 去掉勾选框

  2. zoj4028 LIS

    差分约束瞎搞一下,话说这个数据不知道怎么回事,我的图按道理而言最多只有4n条边,开5n还不够??必须6n?? 约束条件首先根据f函数可建立两点之间的约束,不妨设d[i]为i到0的距离,则对于f[i] ...

  3. Java的基本运算符

    以下内容引用自http://wiki.jikexueyuan.com/project/java/basic-operators.html: Java针对操控变量提供了一组丰富的运算符.可以将所有的Ja ...

  4. sqlite中常见的问题总结

    一.sqlite中不能使用日期进行相减,执行结果无效 例如:SELECT count(*) as cnt FROM DayBanalces WHERE (date(ofDay)- date('2013 ...

  5. JUNIT -- springMVC的action进行单元测试

    原文:http://blog.csdn.net/gaopeng0071/article/details/49946575 我开发环境springMVC版本3.0.4 样例代码: package com ...

  6. HDU 4279 Number(找规律)

    Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. How To Configure a Redis Cluster on Ubuntu 14.04

    原文:https://www.digitalocean.com/community/tutorials/how-to-configure-a-redis-cluster-on-ubuntu-14-04 ...

  8. SQL 约束(Constraints)

    SQL 约束(Constraints) SQL 约束(Constraints) SQL 约束用于规定表中的数据规则. 如果存在违反约束的数据行为,行为会被约束终止. 约束可以在创建表时规定(通过 CR ...

  9. 积跬步,聚小流------ps有用小技巧,改变png图标颜色

    *  实现效果: 原图:  改动后: *  实现目的: 满足为实现不同界面色彩搭配改动png图标的颜色 *  实现方法: 1.打开Photoshop工具,导入须要进行改动的png图标: 2.对导入的图 ...

  10. 我的家乡:三河古镇已经登上央视CCTV-1新闻联播啦!

    在烟雨朦胧时走在古镇的青石街上,别有一番风味!第一幅图为央视的直播车,第二副图为漂亮的三河夜景色!