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 ...
随机推荐
- TCP协议基础知识及wireshark抓包分析实战
TCP相关知识 应swoole长连接开发调研相关TCP知识并记录. 数据封包流程 如图,如果我需要发送一条数据给用户,实际的大小肯定是大于你发送的大小,在各个数据层都进行了数据的封包,以便你的数据能完 ...
- SpringCloud学习(3)——Eureka服务注册中心及服务发现
Eureka概述: Eureka是Netflix的一个子模块, 也是核心模块之一.Eureka是一个基于REST的服务, 用于定位服务, 以实现云端中间层服务发现和故障转移.服务注册与发现对于微服务框 ...
- Centos 修改limits.conf open files后不生效的解决办法
线上几台APACHE服务器报过三.四次open files的问题,导致服务不可用,执行ulimit -n 查看后,发现是默认的1024,找到原因所在了,就去修改下/etc/security/limit ...
- leetcode 刷题日志 2018-03-26
58. 最后一个单词的长度 分析:找最后一个非空格,向前找 int lengthOfLastWord(string s) { int i = s.find_last_not_of(' '); int ...
- 《JavaScript 实战》:实现拖放(Drag & Drop)效果
拖放效果,也叫拖拽.拖动,学名Drag-and-drop ,是最常见的js特效之一.如果忽略很多细节,实现起来很简单,但往往细节才是难点所在.这个程序的原型是在做图片切割效果的时候做出来的,那时参考了 ...
- bootstrap分页查询传递中文参数到后台(get方式提交)
<!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...
- Ubuntu之设置应用开机自启动
前言 前面使用oricle-Linux的时候,设置开机自启动使用的是chkconfig,现在使用ubuntu的时候发现Ubuntu系统没有了RH系统中的 chkconfig命令,因此研究了一下ubun ...
- JS时间转换的一个坑位
在做项目的时候,无意发现了一个小东西. new Date('2018-05-15') new Date('2018-5-15') 输出的结果是不同的,相差了8小时.然后让我回忆到之前看的一个时间转换函 ...
- Java Eclipse 配置
1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...
- ssh使两台机器建立连接
ssh利用口令建立连接过程: 客户端--> 发送连接请求 --> 远程主机 --> 返回远程主机的公钥 --> 公钥加密客户端私钥+客户端公钥返回远程主机 --> 远程主 ...