【题目链接】: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. SignalR -- server push 利器

    实际上关于SignalR的介绍网上有很多,这里不做过多赘述,我们来看下官方网站的描述. [摘录自http://signalr.net/] What is ASP.NET SignalR ASP.NET ...

  2. 在IIS上搭建WebSocket服务器(三)

    编写客户端代码 1.新建一个*.html文件. ws = new WebSocket('ws://192.168.85.128:8086/Handler1.ashx?user=' + $(" ...

  3. nova service-list for juno kilo,liberty openstack

  4. Input 内提示填写内容

    输入框内提示默认内容,用户的点击后提示内容消失,如果填写内容为空,失去焦点后自动显示提示内容 <input type="text" value="搜索" ...

  5. E20170925-hm

    arc  n. 综合症状; 弧(度); 天穹; 电弧,弧光.; vi. 形成拱状物; 循弧线行进; wrap  vt. 包; 缠绕; 用…包裹(或包扎.覆盖等); 掩护;            n. ...

  6. 吝啬的国度 ---用vector 来构图

    根据题目可以看出来  有n 个城市 只有 n-1  条路线 那么  就可以确定这个图中  不存在 圆  所以从一个点到另一个点 只有一条唯一的路  所以从一个节点到另一个节点 那么 这个节点只有一个唯 ...

  7. 【USACO2006 Mar】滑雪缆车 skilift

    [USACO2006 Mar] 滑雪缆车 skilift Time Limit 1000 msMemory Limit 131072 KBytes Description 科罗拉多州的罗恩打算为奶牛建 ...

  8. Http协议详解(转)>>>写的很好

    声明:本片文章非原创,仅供自己学习并分享 内容来源于博客园作者MIN飞翔的HTTP协议详解地址http://www.cnblogs.com/EricaMIN1987_IT/p/3837436.html ...

  9. jQuery Tmpl使用

    1.引入脚本 2.编写模板 2.1假设此时有一个,从后台一json格式发送来的数据 [{"tId":1,"tName":"张三"," ...

  10. Android 从服务器获取时间戳转换为年月日

    用JAVA相关类转换.代码如下: Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(NumberUtils.ge ...