Codeforces 785 D.Anton and School - 2(组合数处理)
[Codeforces 785 D.Anton and School - 2](http://codeforces.com/problemset/problem/785/D)
题目大意:从一串由“(”,“)”组成的字符串中,找出有多少个子序列满足:序列长度为偶数,且前n/2个为“(”,后n/2个为“)”;
思路:枚举每一个左括号,则以该左括号为左右分界的子序列个数为∑C(L-1,i)C(R,i+1)(其中L为该左括号向左的左括号数,R为该左括号向右的右括号数,i从0累加到L-1)。而∑C(L-1,i)C(R,i+1)=∑C(L-1,L-1-i)C(R,i+1)=C(L+R-1,L)。
组合数预处理:
```C++
const int maxn=2e5+10,mod=1e9+7;
ll jc[maxn],inv[maxn];
ll kpow(ll a,ll x)
{
ll res=1;
while (x)
{
if (x&1)
res=res*a%mod;
a=a*a%mod;
x>>=1;
}
return res;
}
void init()
{
jc[0]=1;
for (int i=1;i=0;--i)
{
inv[i]=inv[i+1]*(i+1)%mod;;
}
}
inline ll C(ll n,ll m)
{
if (m>n)
return 0;
return mul(mul(jc[n],inv[m]),inv[n-m]);
}
```
解题代码:
```C++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair P;
typedef map M;
typedef queue Q;
typedef set S;
typedef vector V;
const int maxn=2e5+10,mod=1e9+7;
inline ll add(ll a,ll b)
{
a+=b;
if (a>=mod)
a-=mod;
if (a>=1;
}
return res;
}
void init()
{
jc[0]=1;
for (int i=1;i=0;--i)
{
inv[i]=inv[i+1]*(i+1)%mod;;
}
}
inline ll C(ll n,ll m)
{
if (m>n)
return 0;
return mul(mul(jc[n],inv[m]),inv[n-m]);
}
char s[maxn];
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
int i,j,m,n,k,l=0,r=0;
cin>>s;
n=strlen(s);
init();
for (i=0;i<n;++i)
{
r+=s[i]')';
}
ll ans=0;
for (i=0;i<n;++i)
{
l+=s[i]'(';
r-=s[i]')';
if (s[i]'(')
ans=add(ans,C(l+r-1,l));}
cout<<ans;
return 0;
}
</font>
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
题目链接:http://codeforces.com/contest/785/problem/D 我们可以枚举分界点,易知分界点左边和右边分别有多少个左括号和右括号,为了不计算重复我们强制要求选择分界 ...
- 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 [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...
随机推荐
- lsof---列出当前系统打开的文件信息
lsof---list open file,一个列出当前系统打开文件的工具 1.lsof查找原理 在Linux系统中,系统为了方便管理进程,会在/proc下为每一个运行中的进程创建一个目录,目录名就是 ...
- java----FileInputStream类与FileReader类的区别(转)
FileInputStream类与FileReader类的区别:两个类的构造函数的形式和参数都是相同的,参数为File对象或者表示路径的String,它们到底有何区别呢? Readers and W ...
- Redis之过期策略
一.设置过期时间 Redis对存储值的过期处理实际上是针对该值的键(key)处理的,即时间的设置也是设置key的有效时间.Expires字典保存了所有键的过期时间,Expires也被称为过期字段. e ...
- Excel中把图片合并进图表的方法介绍
方法一: 使用“图案”对话框 双击某个数据系列,选择“图案”标签,单击“填充效果”按钮,在“填充效果”对话框中选择“图片”标签,单击“选择图片”按钮,选择一个要使用的图形文件即可. 方法二: 使用剪贴 ...
- python+opencv+sift环境配置教程
最近在做对应点估计homography,需要用到opencv,c++的接口不如python的接口来的方便 但是在安装python接口的opencv的时候,遇到了各种问题,主要是函数找不到的问题 比如在 ...
- 【转】bitbake 笔记
原文 http://blog.csdn.net/xiaofeng_yan/article/details/6757725 1 当你已经编完一个系统比如sato映像,在编一个meta-toolchain ...
- C++ 新特性 笔记 2 右值引用
C ++ Rvalue引用说明 以下内容,主要是上述链接的摘要 介绍 Rvalue引用是C ++的一个特性,它是随C ++ 11标准添加的.使右值参考有点难以理解的是,当你第一次看到它们时,不清楚它们 ...
- zencart新增categories分类表字段步骤
zencart新增分类字段步骤 1.categories表新增字段related_categories.related_products ) ) NOT NULL; 2.修改admin\categor ...
- netty-3.客户端与服务端通信
(原) 第三篇,客户端与服务端通信 以下例子逻辑: 如果客户端连上服务端,服务端控制台就显示,XXX个客户端地址连接上线. 第一个客户端连接成功后,客户端控制台不显示信息,再有其它客户端再连接上线,则 ...
- string::compare
1. compare string (1) 4int compare (const string& str) const noexcept; substrings (2) int compar ...