【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满足题中所给关系; [题解] 有个 ...
随机推荐
- YTU 2795: 编程题AB-侦察员的密码
2795: 编程题AB-侦察员的密码 时间限制: 1 Sec 内存限制: 128 MB 提交: 155 解决: 43 题目描述 侦察员小甲在被捕前在墙上写了两行文字(ASCII字符),其中包含了他 ...
- 软-RAID 5组建
图文版raid5组建之软RAID [复制链接] 发表于 2007-3-6 09:19 | 来自 51CTO网页 [只看他] 楼主 硬件raid5的组建和使用,基本上说完 ...
- openstack instance resize Resize error: Unable to resize disk down
- poj1006生理周期(中国剩余定理)
生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 139224 Accepted: 44687 Descripti ...
- Tyvj1305最大子序和(单调队列优化dp)
描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...
- Python/Django 下载Excel2003
一.安装 目前支持Excel2003的第三方库多少还有几个,本文使用的是xlwt,安装方式命令行:pip install xlwt 二.使用 首先.引入该库,例如:from xlwt import * ...
- Sorting It All Out 拓扑排序+确定点
这一道题的话 数据有一点问题 ........ 例如 不过 还是 能理解一下 试试吧 ......... A<B B<C C<A A<C B<A ...
- org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException。
jdk1.8环境tomcat运行项目报错, org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException.解决方法:更改jdk1.7
- lua_string_pattern
两大特点: 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作字符串,而是返回一个新的字符串. 库函 ...
- UVM基础之--------uvm_root
uvm_root 是uvm的顶层实例扮演了一个top-level and phase controller 的作用,对于component来说.该类不需要用户实例化,他是一个自动实例化了的类,用户直接 ...