传送门

题意

给出一个只包含'(',')'的字符序列,询问有多少个\(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. 通过分析system_call中断处理过程来深入理解系统调用

    通过分析system_call中断处理过程来深入理解系统调用 前言说明 本篇为网易云课堂Linux内核分析课程的第五周作业,上一次作业中我以2个系统调用(getpid, open)作为分析实例来分析系 ...

  2. poj3648,2-sat求解

    关键是题意的理解,英语,有时候明明每个字都认识,但是还是理解错误!哎!!悲剧啊!题意啊! 这是关键!开始误理解为n对新娘郞,非也!是只有一对,其他是夫妇,理解后就好做了,建立图 是关键,怎么转化关系, ...

  3. 前端学习之--CSS

    CSS 常用帮助文档 CSS:被用来同时控制多重网页的样式和布局.HTML页面中CSS样式的写法有3种: 1:标签中写入 <body style='margin o auto;'> 2:h ...

  4. HDU 4738 割边

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. BZOJ——T 1707: [Usaco2007 Nov]tanning分配防晒霜

    http://www.lydsy.com/JudgeOnline/problem.php?id=1707 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 8 ...

  6. 学习swift从青铜到王者之Swift语言函数05

    1.定义一个函数以及调用 //一,定义一个无参无返回值函数 func fun1(){ print("this is first function") } fun1() 2.定义一个 ...

  7. 【深入探索c++对象模型】data语义学二

    单一继承中,base class 和derived class的对象都是从相同的地址开始,其间差异只在于derived class比较大,用以容纳自己的nonstatic members. 若vptr ...

  8. Malformed or corrupted AST file: &#39;Unable to load module &quot;...

    Malformed or corrupted AST file: 'Unable to load module "/Users/topbar/Library/Developer/Xcode/ ...

  9. 使用Java对100以内偶数求和

    /** * 根据for循环的描述: for(变量初始化:循环条件:修改循环变量的值),求出100以内的所有偶数,for(int i = 0; i<=100; i+=2),把这些偶数累加到一个空的 ...

  10. Android多线程更新UI的方式

    Android下,对于耗时的操作要放到子线程中,要不然会残生ANR,本次我们就来学习一下Android多线程更新UI的方式. 首先我们来认识一下anr: anr:application not rep ...