[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(组合数处理)的更多相关文章

  1. Codeforces 785 E. Anton and Permutation(分块,树状数组)

    Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...

  2. Codeforces 785 D. Anton and School - 2

    题目链接:http://codeforces.com/contest/785/problem/D 我们可以枚举分界点,易知分界点左边和右边分别有多少个左括号和右括号,为了不计算重复我们强制要求选择分界 ...

  3. CodeForces 785 D Anton and School - 2 范德蒙恒等式

    Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include ...

  4. Codeforces 785 - A/B/C/D/E - (Undone)

    链接:https://codeforces.com/contest/785 A - Anton and Polyhedrons #include<bits/stdc++.h> using ...

  5. 【codeforces 785E】Anton and Permutation

    [题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...

  6. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  7. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 785C】Anton and Fairy Tale

    [题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...

  9. 【codeforces 785B】Anton and Classes

    [题目链接]:http://codeforces.com/contest/785/problem/B [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...

随机推荐

  1. SQLServer 导入大容量sql文件

    cmd命令行,管理员身份运行 执行以下语句:E:\dbbak\abhs\SmartEnglish_data.sql 为文件路径,AbhsEnglish 为要导入的数据库 sqlcmd -i E:\db ...

  2. vb.net DBEntities框架联表查询 Join

    在项目中配置好DBEntities 使用两个表:主表Table, 子表Table_Item 主要是用到了委托和泛型,ForEach用的是不带返回值的委托 Sub GetDb() Dim st As N ...

  3. 【原创】Linux基础之logrotate

    logrotate logrotate ‐ rotates, compresses, and mails system logs logrotate is designed to ease admin ...

  4. 【题解】P3391 文艺平衡树

    用pb_ds库中的rope水过去的,忽然发现这玩意能水好多模拟题. 详见这个博客:背景的小姐姐真的好看 声明 #include <ext/rope> using namespace __g ...

  5. char 、 unsigned char 互相转化

    1. 利用unsigned char (即uchar) 保存char 数据  ,直接赋值即可 unsigned char uc; char c=-33; uc= c; cout<<(int ...

  6. java实现spark常用算子之Repartitions

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  7. Java设计模式只好

    有时,一些学生私下问我:如何学习前端问题.这里有一个统一的回复,下次我遇到这个问题,同学们会直接给你发这篇文章的链接地址. “如何学习前端”应该因人而异,其他人的方法可能不适合自己.让我们谈谈我的学习 ...

  8. JVM学习笔记(一):Java虚拟机和虚拟机内存区域

    为什么Java程序需要运行在虚拟机上 因为Java在设计之初的跨平台特性,我们知道Java程序是运行在Java虚拟机上的.如果你要问为什么Java程序要运行在虚拟机上,我可以反问你几个问题. 为什么买 ...

  9. idea 党用快捷键

    实用快捷键: Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*...*/ )Ctrl+D 复制行Ctrl+X 删除行快速修复 alt+enter (modify/cast)代码提示 a ...

  10. https和http的post发送总结

    本文为转贴内容,感谢作者阿进! 需要转发数据到客户的https的服务器上出现一系列问题总结如下: 1.因为是https首先考虑到用最新的控件NetHTTPClient(只有在XE8以上才有). 2.客 ...