CodeForces 785D Anton and School - 2
枚举,容斥原理,范德蒙恒等式。
先预处理每个位置之前有多少个左括号,记为$L[i]$。
每个位置之后有多少个右括号,记为$R[i]$。
然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的方案数。
即在它左边取$k$个左括号,在右边取$k-1$个右括号都是合法的方案,这个东西可以用范德蒙恒等式化成一个组合数以及容斥原理计算。
范德蒙恒等式:http://blog.csdn.net/acdreamers/article/details/31032763
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL; char s[200010];
long long L[200010],R[200010];
LL fac[200010];
long long mod = 1e9+7; void init()
{
int i;
fac[0] =1;
for(i =1; i <= 200000; i++)
fac[i] = fac[i-1]*i%mod;
}
LL pow(LL a, LL b)
{
LL tmp = a % mod, ans =1;
while(b)
{
if(b &1) ans = ans * tmp % mod;
tmp = tmp*tmp % mod;
b >>=1;
}
return ans;
}
LL C(LL n, LL m)
{
if(m>n||m<0)return 0;
return fac[n]*pow(fac[m]*fac[n-m],mod-2)%mod;
} int main()
{
init();
scanf("%s",s);
int len = strlen(s); for(int i=1;i<=len;i++)
{
L[i] = L[i-1];
if(s[i-1]=='(') L[i]++;
} for(int i=len;i>=1;i--)
{
R[i] = R[i+1];
if(s[i-1]==')') R[i]++;
} long long ans = 0; for(int i=1;i<=len;i++)
{
if(s[i-1]=='(') continue;
if(L[i]==0) continue; long long m,k1,k2; m = min(L[i],R[i]);
if(m==0) k1=1;
else k1 = C(L[i]+R[i],m); m = min(L[i],R[i]-1);
if(m==0) k2=1;
else k2 = C(L[i]+R[i]-1,m); long long k = (k1-k2+mod)%mod;
ans = ( ans + k ) %mod;
} printf("%lld\n",ans); return 0;
}
CodeForces 785D Anton and School - 2的更多相关文章
- Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)
D. Anton and School - 2 time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
- Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...
- Codeforces 785D Anton and School - 2(组合数)
[题目链接] http://codeforces.com/problemset/problem/785/D [题目大意] 给出一个只包含左右括号的串,请你找出这个串中的一些子序列, 要求满足" ...
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- CodeForces 785D Anton and School - 2 (组合数学)
题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)
大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
随机推荐
- linux shell学习一
本博客参考自: http://www.cnblogs.com/waitig/p/5523409.html <shell从入门到精通> 张春晓编著 Shell简介 Shell自身是一个用C ...
- C语言 结构体作为参数和返回值使用
方案一:结构体变量作为参数,进行传值. 编译器需要拷贝,不影响origin value,使用成员操作符(.)直接访问 /**************************************** ...
- html常见页面动画插件
wow.js页面滚动动画 scrollever.js页面滚动动画 textillate.js页面字体效果 ihover.js手表hover效果插件 loaders.css页面预加载动画
- 不管谁坐了CIO的位置 都必须了解的法则
目前一些设立了CIO岗位的央企中,CIO也只做到了“IO”(信息官,Information Officer),而没有做到“C”(首席,Chief).老总们总在抱怨没有合适的人选:懂技术的不懂业务,懂业 ...
- 针对移动设备的CSS3布局
针对移动设备的CSS3布局 一些专业人士预测五年内移动设备将击败普通电脑成为网页浏览领域的霸主,不管这个预言是否应验,让网页在移动设备上较好的显示已经成为网页设计师和开发者的重要任务,本教程学习用CS ...
- 【洛谷 P4219】 [BJOI2014]大融合(LCT)
题目链接 维护子树信息向来不是\(LCT\)所擅长的,所以我没搞懂qwq 权当背背模板吧.Flash巨佬的blog里面写了虽然我没看懂. #include <cstdio> #define ...
- C#里面中将字符串转为变量名
public partial class Form1 : Form { string str = "spp"; public string spp = "very goo ...
- Codeforces Round #466
A. Points on the line 题意 给定一条直线上\(n\)个点,要求去掉最少的点,使得直线上相距最远的两个点的距离\(\leq d\). 思路 枚举长度为\(d\)的区间. Code ...
- 21.Merge Two Sorted Lists---《剑指offer》面试17
题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/ 题目大意: 给出两个升序链表,将它们归并成一个链表,若有重 ...
- Jmeter命令行选项
示例:jmeter.bat -n -j %tmp%\%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%%time:~ ...