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 [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...
随机推荐
- c#md5加密的简单用法
using System.Security.Cryptography; //MD5 md5 = MD5.Create(); MD5 md5 = new MD5CryptoServiceProvider ...
- 结合sklearn的可视化工具Yellowbrick:超参与行为的可视化带来更优秀的实现
https://blog.csdn.net/qq_34739497/article/details/80508262 Yellowbrick 是一套名为「Visualizers」的视觉诊断工具,它扩展 ...
- Eloquent JavaScript #06# class
索引 Notes this Prototype 类 class符号 覆盖派生属性 Maps Symbols iterator接口 Getters, setters, and statics 继承 in ...
- php 获取最近一周,一个月,一年
<?php date_default_timezone_set('PRC'); /** * 获取最近一周,一个月,一年 * */ function getLatelyTime($type = ' ...
- Spring Boot(十六):使用Jenkins部署Spring Boot
Spring Boot(十六):使用Jenkins部署Spring Boot jenkins是devops神器,介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署 ...
- sentos7为例添加python3和python2共存
转载:https://www.cnblogs.com/JahanGu/p/7452527.html 1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命 ...
- http请求记录
Request Headers 请求头 Content-Type 默认值: "application/x-www-form-urlencoded".发送信息至服务器时内容编码类型 ...
- Codeforces 832E Vasya and Shifts - 高斯消元
题目传送门 快速的传送门I 快速的传送门II 题目大意 (题意比较复杂,请自行阅读原题) 可以将原题的字母都看成它们的在字符表中的下标,这样问题就变成给定$n$个$m$维向量$\vec{a_{1}}, ...
- Ubuntu + CUDA9 + CUDNN7 + OpenCV3.4 + contrib +CAFFE-master
安装ubuntu时赞美Rufus(建议ubuntu16.04.04),过程参考 https://www.cnblogs.com/willnote/p/6725594.html 安 装 前 一 定 要 ...
- dart基础计数器
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...