[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. 《深入实践C++模板编程》之一——Hello模板

    1.通过一个简单的例子来理解模板的用途: 模板为不同类型的数据生成操作相同或相似的函数. 弱语言如Python,可以使用一种函数来应对各种类型,但是C++就不得不为不同的类型编写相似的函数.模板的作用 ...

  2. dev GridView 的组计和分组计

    /// <summary> /// //添加组计 /// </summary> private void SetGroupSummary(GridView gv, string ...

  3. Line 算法与deepwalk的对比 和个人理解

    用户的关注关系本身就是一个图结构,要从用户关注关系生成用户的embedding表示,其实就是做graph的emebding表示. deepwalk+word2vec 比较简单,效果也还可以.这种方法再 ...

  4. mysql 关于字符串搜索的函数

    与like作用相似 FIND_IN_SET LOCATE POSITION

  5. 第五章· MySQL数据类型

    一.数据类型介绍 1.四种主要类别  1)数值类型 2)字符类型 3)时间类型 4)二进制类型 2.数据类型的 ABC 要素 1)Appropriate(适当) 2)Brief(简洁) 3)Comp ...

  6. css文字样式与div

    文字与图片 如果要要将字移动到图片的上方,这里就需要定位一下,设置div为父级,为相对定位:设置h1为绝对定位: div{position:relative;} h1{font-size:16px;c ...

  7. jsp学习——九大内置对象

    JSP一共有九个内置对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 博客:JSP的九 ...

  8. QTP(1)

    一.概念 1.什么是软件测试? 使用人工或者自动手段来运行或者测试某个软件的过程,其目的在于检验程序是否满足需求规格说明书或者弄清实际结果与预期结果之间的差异. (1)软件(程序+文档+数据)测试 ( ...

  9. php 垃圾处理机制

    在php5.3版本之前, php变量的回收机制只是简单的通过计数来处理(当refcount=0时,会回收内存),但这样会出现一个问题 $a=array("str"); $a[]=& ...

  10. php正则表达式提取数字,字符串中提取数字

    <?php $str = "请注意:有谁知道30901.5号路怎么走?这个因为我买了100块的烧饼和7901的钥匙了,那个对了,我再拿个30000"; $pattern = ...