CF785D Anton and School - 2
problem
给出一个括号序列,要求删除一些括号使得剩下的括号序列是个匹配的括号序列,且改括号序列左边全部为左括号,右边全部为右括号。
solution
考虑枚举左右括号交界的位置\(x\),为了避免重复计算,强制要求\(x\)左边的第一个左括号必选。然后枚举\(x\)的时候只枚举左括号的位置。
然后枚举括号序列的长度。假设长度为\(2i\),那么左右括号就分别有\(i\)个,假设左边有\(n\)个左括号,右边有\(m\)个右括号。那么该位置的答案就是\(\sum\limits_{i=1}^{min(n,m)}C_{n-1}^{i-1}C_{m}^i\)
观察上面这个式子,当\(i=0\)时没有贡献,所以我们可以等价的写成\(\sum\limits_{i=0}^{min(n,m)}C_{n-1}^{i-1}C_m^i\)
假设\(n\le m\)
上面的式子也可以写成
\(\sum\limits_{i=0}^nC_{n-1}^{n-i}C_m^i\)
考虑这个东西的组合意义,也就相当于有\(n+m-1\)个物品从中选\(n\)个。
所以上面的东西其实就是\(C_{n+m-1}^n\)然后就可以\(O(1)\)求了。
可以发现,当\(m\le n\)时,推出的式子也是这个。
这样总复杂度就成了\(O(n)\)
code
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n;
const int N = 200100;
char s[N];
int cnta,cntb,jc[N],inv[N];
int C(int x,int y) {
return 1ll * jc[x] * inv[y] % mod * inv[x - y] % mod;
}
int qm(int x,int y) {
int ret = 1;
for(;y;y >>= 1,x = 1ll * x * x % mod) {
if(y & 1) ret = 1ll * ret * x % mod;
}
return ret;
}
int main() {
scanf("%s",s + 1);
n = strlen(s + 1);
jc[0] = 1;
for(int i = 1;i <= n;++i) jc[i] = 1ll * jc[i - 1] * i % mod;
for(int i = 0;i <= n;++i) inv[i] = qm(jc[i],mod - 2);
for(int i = 1;i <= n;++i) if(s[i] == ')') cntb++;
ll ans = 0;
for(int i = 1;i <= n;++i) {
if(s[i] == '(') {
cnta++;
ans += C(cnta + cntb - 1,cnta);
ans %= mod;
}
else cntb--;
}
cout<<ans;
return 0;
}
CF785D Anton and School - 2的更多相关文章
- CF785D Anton and School - 2 解题报告
CF785D Anton and School - 2 题意:给定一个长度\(\le 2 \times 10e5\)由'('和')'组成的字符串,问有多少个子串(可以不连续),前半部分是由\('('\ ...
- CF785D Anton and School – 2
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 水题
D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- Codeforces Round #379 (Div. 2) A. Anton and Danik 水题
A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...
随机推荐
- c#串口通信并处理接收的多个参数
最近摸索做个上位机,简单记录一下关键的几个部分 c#做串口通信主要使用的是System.IO.Ports类,其实还是十分方便的 最终效果如下: 千万不要忘记了下面这个 填写串口相关配置 我们可以通过G ...
- tp、tftp、nfs--服务器搭建
服务器 1. ftp服务器 1.1检查是否安装 vsftpd -version 1.2 安装 sudo apt-get install vsftpd 1.3卸载 sudo apt-get remove ...
- centos7在线yum安装mysql时官方镜像下载过慢的解决方案
帮客户调试数据库,搭建一测试环境,centos7最小化安装后,在线安装mysql. 步骤: 1. wget -i http://dev.mysql.com/get/mysql57-community- ...
- 记录python上传文件的坑(2)
描述: 1.之前在写项目mock代码时,碰到一个上传文件的接口,但项目接口本身有token保护机制,碰到token失效时,需要重新获取一次token后,再次对上传文件发起请求,在实际调用中发现,第一次 ...
- 《HTTPS权威指南》读书笔记——SSL/TLS协议
记录协议(record protocol) 负责在传输连接上交换所有底层信息 每一条记录以短标头开始,标头包含记录内容的类型.协议版本和长度 握手协议(handshake protocol) 整个过程 ...
- js全局属性/函数
全局函数 eval () isFinite () 检查某个值是否为有穷大的数 isNaN () 检查某个值是否是数字 返回true或fa ...
- Glide生命周期原理
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/uTv44vJFFJI_l6b5YKSXYQ作者:连凌能 Android App中图片的展示是很 ...
- SpringMVC参数绑定,这篇就够了!
SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了 下面通过5个常用的注解演示下如何进行 ...
- Web前端基础(18):jQuery基础(五)
1. 事件 前面我们介绍完js的事件流的概念之后,相信大家对事件流也有所了解了.那么接下来我们看一下jquery的事件操作. 在说jquery的每个事件之前,我们先来看一下事件对象 . 1.1 事件对 ...
- Eclipse:批量将Java源代码文件的编码从GBK转为UTF-8
很简单的几行代码,就可以批量将GBK格式的java文件转为UTF-8格式. 基本上所有文本文件的编码转换都可以采用这种方式. import java.io.File; import java.io.I ...