Codeforces785D - Anton and School - 2
传送门
题意
给出一个只包含'(',')'的字符序列,询问有多少个\(RSBS\)
分析
首先需要知道一个公式
\]
接下来我们观察第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的更多相关文章
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- 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 ...
- 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 ...
随机推荐
- Flex里监听mouseDownOutside事件解决弹出窗口点击空白关闭功能
其实当用户在使用 PopUpManager 打开的某个组件外部单击时,会从该组件分派一个mouseDownOutside事件 监听该事件就能实现点击空白处关闭窗口的功能 this.addEventLi ...
- Python高级进阶(二)Python框架之Django写图书管理系统(LMS)
正式写项目准备前的工作 Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使用之前就我 ...
- Vue基础学习
使用vue-cli构建初始化vue项目 vue init webpack myfirst 项目截图:(开发工具:webStorm) 主要练习了vue的基本指令:v-bind.v-if.v-show.v ...
- Ubuntu 16.04安装Fiddler抓包工具(基于Mono,且会有BUG)
说明:Fiddler官方提供了Mono版本的,但是只有2014版本的,不是最新的,并且运行期间会有BUG,比如界面错乱卡死等等,但是勉强能代理,抓SSL的包,如果使用了要做好心理准备.将就一下还是可以 ...
- Simics 破解 转
http://www.eetop.cn/blog/html/28/1066428-type-bbs-view-myfav.html http://blog.sina.com.cn/s/blog_538 ...
- maven的超级pom
对于 Maven3,超级 POM 在文件 %MAVEN_HOME%/lib/maven-model-builder-x.x.x.jar 中的 org/apache/maven/model/pom-4. ...
- spring mvc 整理
spring mvc 整理
- Java中常见的排序算法
这是我摘取的一段英文资料.我认为学习算法之前,对各种排序得有个大致的了解: Sorting algorithms are an important part of managing data. At ...
- 关于disable和readonly
我们在做网页时,难免的会因为权限或者各种原因,想让使用者看到,但是却不想让用户去对值进行更改,我们有两个选择 一.我们使用disabled将文本框禁用掉. 二.我们使用readonly使得文本框只能读 ...
- 【转】TestNG常用注解
http://blog.csdn.net/d6619309/article/details/52435084 TestNG的注解大部分用在方法级别上.常用的注解列举如下: 1. Before类别和Af ...