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 ...
随机推荐
- react-native安装react-navigation后出现package-lock.json文件的坑
npm5.0开始安装后回生成一个新的package-lock.json文件.以致初始化好的react-native项目引入的依赖被删除. 目前解决办法.使用facebook的yarn add 第三方组 ...
- 前端PHP入门-002-安装WAMP的集成环境md
> 第一次讲PHP,让我感觉还是满好玩的,一种新的知识的学习,需要我们努力! > 这次PHP课程计划是15天快速入门的课程! 只是单独的讲PHP语言,不涉及很深的内容,只是想让web前端的 ...
- C++字符串使用sizeof时注意
] = {"hello,你好"}; char tmp2[] = {"hello,你好"}; ]; sprintf(tmp3,"%s",&qu ...
- JS中的匿名函数自执行、函数声明与函数表达式
先看一段jQuery源码中匿名函数自执行的例子: (function( window, undefined ) { // jquery code })(window); 另外一种常见的写法: +fun ...
- NAIPC2018-K-Zoning Houses
题目描述 Given a registry of all houses in your state or province, you would like to know the minimum si ...
- leetcode 刷题日志 2018-03-26
58. 最后一个单词的长度 分析:找最后一个非空格,向前找 int lengthOfLastWord(string s) { int i = s.find_last_not_of(' '); int ...
- CS48 D BIT
统计一个点对应的和它严格右下方的点,点对数量.由于数据规模很大,不能直接上二维的前缀和,先排一维序,然后用BIT维护前缀和即可. /** @Date : 2017-09-14 20:17:30 * @ ...
- CodeForces - 1015D
There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially ...
- python 输出 a+b
AC代码: 单组输入: s=input().split() print(int(s[0])+int(s[1]))
- 自定义 feign 反序列化时间字符格式
参考 : https://blog.csdn.net/forezp/article/details/73480304 feign client 默认配置类:默认的配置类为FeignClientsCon ...