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 ...
随机推荐
- 前端PHP入门-014-参数的引用
我们学习了变量的引用,我们来回顾一下知识: <?php $a = 10; $b = &$a; $a = 100; echo $a.'---------'.$b; ?> 而函数的参数 ...
- OpenCV---色彩空间(一)
颜色空间:用三种或者更多特征来指定颜色的方法,被称为颜色空间或者颜色模型 1.RGB(OpenCV中为BGR): 一幅图像由三个独立的图像平面或者通道构成:红.蓝.绿(以及可选项:透明度alpha通道 ...
- 计算方法 -- 解线性方程组直接法(LU分解、列主元高斯消元、追赶法)
#include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> ...
- 基础但是很重要的2-sat POJ 3678
http://poj.org/problem?id=3678 题目大意:就是给你n个点,m条边,每个点都可以取值为0或者1,边上都会有一个符号op(op=xor or and三种)和一个权值c.然后问 ...
- Linux系统调用和库函数
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unist ...
- HTML入门(一)
---恢复内容开始--- HTML 一 .HTML介绍 1. 什么是HTML? 超文本标记语言: 超文本: 比普通文本更强大 标记语言: 使用一组标签对内容进行描述的一门语言,它不是编程语言! 2. ...
- H5小游戏——看你有多色
使用了封装了canvas的create.js库来实现的. 最终效果: 工程: Rect.js /* * 方块类 */ function Rect(n,color,specialColor){ crea ...
- VC字体对话框的初始化
本代码需要先添加类成员 LOGFONT lf; void CMyDlg::OnButton3() { // TODO: Add your control notification handler c ...
- 使用Burpsuite爆破弱口令教工号
使用Burpsuite爆破弱口令教工号 发表于 2015-11-18 | 分类于 Burpsuite | 1条评论 | 26次阅读 准备 所谓工欲善其事,必先利其器,首先当然是要下 ...
- elk系列1之入门安装与基本操作【转】
preface 我们每天都要查看服务器的日志,一方面是为了开发的同事翻找日志,另一方面是巡检服务器查看日志,而随着服务器数量以及越来越多的业务上线,日志越来越多,人肉运维相当痛苦了,此时,参考现在非常 ...