【题目链接】:http://codeforces.com/contest/785/problem/D

【题意】



给你一个长度为n的括号序列;

让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后x个括号为右括号;

问你有多少种删除方法.

【题解】



先考虑一个简单的问题

对于一个括号序列,如果前x个括号都是左括号,后y个括号都是右括号;

((())))

那么这个序列的RSBS的个数为C(X+Y,X)

即相当于构造一个长度为x+y的序列,包含x个1,y个0

如上述括号序列



这里即构造一个长度为7的数字序列,里面含有3个1和4个0

这里选择0对应的左括号和1对应的右括号;

可以发现它们能够构成满足要求的序列

如上图的绿色部分;

这里相当于确定有几个左括号被抵消;

假如有1个左括号对应的数字是1,那么就相应的有3-1个右括号对应的数字是1;

这样刚好和两个左括号对应;

很巧妙的方法吧。

所以这个序列的答案就是C(7,3)

当然上面说的是简化版本的问题;

考虑完整的问题;

我们可以枚举每一个左括号

假设这个左括号是最后的RBSB的括号序列的最后一个左括号;

即这个左括号左边只能是左括号,右边只能是右括号了;

则

我们用前缀和处理出这个左括号前面有多少个左括号,这个左括号后面有多少个右括号;

分别设为x和y;

瞧;

这就变成我们刚才所讨论的问题了;

则答案应该是C(X+Y-1,X),因为这个时候已经确定有一个左括号(即枚举的这个最后一个左括号已经确定了),所以只剩下x+y-1个位置来放那个1了;

这里X+Y会很大;不能写递推式;

只能先求出1..200000的阶乘模和对应的乘法逆元;

然后用C(N,M)=N!/((N-M)!M!)来算



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5 + 100;
const LL MOD = 1e9 + 7; LL fac[N],inv_fac[N],ans;
int l[N], r[N],n;
char s[N]; LL ksm(LL x, LL y)
{
if (y == 1) return x;
LL temp = ksm(x, y >> 1);
temp = (temp*temp)%MOD;
if (y & 1)
temp = (temp*x) % MOD;
return temp;
} LL C(LL n, LL m)
{
if (n < m) return 0;
return (((fac[n] * inv_fac[n - m]) % MOD)*inv_fac[m]) % MOD;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
fac[0] = 1, inv_fac[0] = 1;
rep1(i, 1, 2e5)
fac[i] = (1LL * fac[i - 1] * i) % MOD, inv_fac[i] = ksm(fac[i], MOD - 2);
scanf("%s", s + 1);
n = strlen(s + 1);
rep1(i, 1, n)
if (s[i] == '(')
l[i] = l[i - 1] + 1;
else
l[i] = l[i - 1];
rep2(i, n, 1)
if (s[i] == ')')
r[i] = r[i + 1] + 1;
else
r[i] = r[i + 1];
rep1(i,1,n)
if (s[i] == '(')
{
ans = (ans + C(l[i] + r[i] - 1, l[i]))%MOD;
}
printf("%lld\n", ans);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 785D】Anton and School - 2的更多相关文章

  1. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【13.77%】【codeforces 734C】Anton and Making Potions

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【77.39%】【codeforces 734A】Anton and Danik

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【25.00%】【codeforces 584E】Anton and Ira

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  8. 【codeforces 785E】Anton and Permutation

    [题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...

  9. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

随机推荐

  1. cookie domain and cookie path

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie Domain=<domain-value> Opt ...

  2. ASP.NET Overview

    https://msdn.microsoft.com/en-us/library/4w3ex9c2.aspx ASP.NET is a unified统一的 Web development model ...

  3. P4111 [HEOI2015]小Z的房间 生成树计数

    这个题是生成树计数的裸题,中间构造基尔霍夫矩阵,然后构成行列式,再用高斯消元就行了.这里高斯消元有一些区别,交换两行行列式的值变号,且消元只能将一行的数 * k 之后加到别的行上. 剩下就没啥了... ...

  4. Makefile 实际用例分析(二) ------- 比较通用的一种架构

    之前已经讲了这一篇文章:Makefile实际用例分析(一)-----比较通用的一种架构 现在这篇其实和那个差的不是很多,只是在布局上有些差别(这个makefile也是论坛上一起讨论过的,囧,忘了哪个论 ...

  5. compileSdkVersion, minSdkVersion 和 targetSdkVersion的选择(copy)

    英文原文:Picking your compileSdkVersion, minSdkVersion, and targetSdkVersion 作者:Ian Lake,Google Android ...

  6. PCB Genesis 外形加内角孔实现方法

    在PCB工程制作CAM时,经常会遇到外形拐角处直角的,而客户对内角是要求,比如最大内角要求R0.5mm或者不接受内角, 但成型方式为铣方式,又不是啤板成型,那怎么处理才可以达到要求效果呢,在这里介绍2 ...

  7. 【废弃】【WIP】JavaScript 函数

    创建: 2017/10/09 更新: 2017/11/03 加上[wip] 废弃: 2019/02/19 重构此篇.原文归入废弃  增加[废弃中]标签与总体任务 结束: 2019/03/12 完成废弃 ...

  8. 解决Sublime Text 3 的 Package Control 启动失败问题

    今天在使用Sublime Text的时候,需要了这样的情况         遇到这个问题的时候   我是这样解决的 一. 首先 找到 Package Control的下载地址1  下载地址2.将下载下 ...

  9. JavaScript--改变 HTML 样式

    HTML DOM 允许 JavaScript 改变 HTML 元素的样式.如何改变 HTML 元素的样式呢? 语法: Object.style.property=new style; 注意:Objec ...

  10. 网上流行的学生选课相关的50个常用sql语句

    学生表 Student(S#,Sname,Sage,Ssex) 教师表 Teacher(T#,Tname) 课程表 Course(C#,Cname,T#) 学生成绩表 SC(S#,C#,score) ...