Codeforces 785 D. Anton and School - 2
题目链接:http://codeforces.com/contest/785/problem/D
我们可以枚举分界点,易知分界点左边和右边分别有多少个左括号和右括号,为了不计算重复我们强制要求选择分界点左边的那一个左括号(也就是说如果枚举的这个分界点的左边这个位置没有左括号就强制这个位置不产生贡献)。
对于一个分界点我们记它左边有$le[x]$个左括号,右边有$ri[x]$个右括号。
${Ans=\sum_{x=1}^{n-1} \sum _{i=1}^{min(le[x]-1,ri[x]])}C(le[x]-1,i)*C(r[x],i)}$
${=\sum_{x=1}^{n-1} C(le[x]-1+ri[x],ri[x])}$
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1000010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
#define md 1000000007
llg n,m,le[maxn],ri[maxn],inv[maxn],fac[maxn]; char c[maxn]; llg ksm(llg a,llg b)
{
llg ans=;
while (b)
{
if (b&) ans*=a,ans%=md;
a*=a,a%=md;
b/=;
}
return ans;
} llg C(llg N,llg M)
{
if (M==) return ;
return fac[N]*inv[M]%md*inv[N-M]%md;
} int main()
{
yyj("D");
scanf("%s",c+);
n=strlen(c+);
llg p=;
for (llg i=;i<=n;i++)
{
if (c[i]=='(') p++;
le[i+]=p;
}
p=;
for (llg i=n;i>=;i--)
{
if (c[i]==')') p++;
ri[i]=p;
}
fac[]=;
llg ans=;
for (llg i=;i<=n*+;i++)
fac[i]=fac[i-]*i%md,inv[i]=ksm(fac[i],md-);
// fac[0]=0;
for (llg i=;i<=n;i++)
if (le[i] && ri[i] && c[i-]=='(')
ans+=C(le[i]+ri[i]-,ri[i]-),ans%=md;
cout<<ans;
return ;
}
Codeforces 785 D. Anton and School - 2的更多相关文章
- Codeforces 785 D.Anton and School - 2(组合数处理)
Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- CodeForces 785 D Anton and School - 2 范德蒙恒等式
Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include ...
- Codeforces 785 - A/B/C/D/E - (Undone)
链接:https://codeforces.com/contest/785 A - Anton and Polyhedrons #include<bits/stdc++.h> using ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 785C】Anton and Fairy Tale
[题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...
- 【codeforces 785B】Anton and Classes
[题目链接]:http://codeforces.com/contest/785/problem/B [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...
随机推荐
- 基于jquery、bootstrap的数据验证插件bootstrapValidator使用
实时验证用户名是否存在,密码不能和用户名相同,两次密码需要相同,提交之后需要验证返回值: <form id="defaultForm" role="form&quo ...
- <转>jmeter(十一)JDBC Request之Query Type
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- serving inference
1.确定要提供服务的inference的input,output,以及exporter的signature:(这里用classify的signature做例子,input为byte数组,output为 ...
- 浏览器从输入URL到页面加载显示完成全过程解析
一 浏览器查找域名对应的 IP 地址(域名解析的过程,先进行缓存的查看): 1.在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用 ...
- 第二节 JavaScript基础
JavaScript组成及其兼容性: ECMAScript:解释器,翻译,用于实现机器语言和高级语言的翻译器:几乎没有兼容性问题 DOM(Document Object Model):文档对象模型,文 ...
- Mongo第三个参数的用法
Mongo update的用法 Update( array $criteria , array $new_object [, array $options = array() ] ) 第一个参数是条件 ...
- Kattis之旅——Rational Arithmetic
Input The first line of input contains one integer, giving the number of operations to perform. Then ...
- PyCharm 设置护眼背景色
PyCharm 设置护眼背景色 一.方法 File -> Seting -> Editor -> Color Scheme -> General -> Text -> ...
- intel FPGA使用
https://www.altera.com/documentation/swn1503506366945.html https://files.cnblogs.com/files/shaohef/o ...
- mysql 5.6二进制安装
1. 进入用户家目录创建tools文件夹 # cd ~ #mkdir tools 2.下载MySQL5.6二进制包(搜狐镜像里有各版本下载http://mirrors.sohu.com/mysql/ ...