【codeforces 785D】Anton and School - 2
【题目链接】: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的更多相关文章
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
随机推荐
- bzoj 1036 树的统计Count
题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u ...
- bag of words in c++
#include <iostream> #include <vector> #include <cstddef> #include <string> # ...
- discuz的全局变量
$_G 保存了 discuz! 中所有的预处理数据 缓存能够很好的提高程序的性能,一些配置数据没必要每次都查询数据库,只要在修改了的时候更新下缓存即可. Discuz! 中所有的缓存保存在 $_G[c ...
- 关于 node.js 小插曲
随着web2.0的时代到来,javascript在前端担任了更多的职责,事件也看得到了广泛的应用,node不像rhino那样受java的影响很大,而是将前端浏览器中应用广泛企鹅成熟的事件引入后端,配合 ...
- [Apple开发者帐户帮助]九、参考(6)支持的功能(watchOS)
watchOS扩展可用的功能取决于您的程序成员身份. 注意:对于watchOS应用程序目标,可用的功能是应用程序组和后台模式,并且不依赖于您的程序成员身份. 能力 ADP 企业 Apple开发者 应用 ...
- Swagger 教程
转自 Vojtech Ruzicka的编程博客 (一)Swagger和SpringFox 记录REST API非常重要.它是一个公共接口,其他模块,应用程序或开发人员可以使用它.即使你没有公开曝光 ...
- Mac 的可清除空间(时间机器)
最近项目引入新技术flutter 所以需要更新xcode,下载完了xcode,安装不上 ,费解半天,提示磁盘空间不足.如下图,看到剩余九十多个G, 怎么都解决不了这个问题 就是买磁盘情理软件clean ...
- ACM_滚动AC
滚动AC Time Limit: 2000/1000ms (Java/Others) Problem Description: 小光最近拉了几个同学入ACM的坑,为鼓励A题,就增加奖励制度:每AC三道 ...
- 通过yum命令搭建lamp环境(centos6.5)
centos 6.5 1.yum安装和源代码编译在使用的时候没啥区别,但是安装的过程就大相径庭了,yum只需要3个命令就可以完成,源代码需要13个包,还得加压编译,步骤很麻烦,而且当做有时候会出错,源 ...
- 右边根据左边的高度自动居中只需要两行CSS就可以完成
右边根据左边的高度自动居中只需要两行CSS就可以完成 <style type="text/css" > div{ display: inline-block; vert ...