[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. 浅谈JMX

    JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构.设计模式.应用程序接口以及服务.通常使用JMX来监控系统的运行状态或管理系统的某些方面,比如清空缓存.重新加载配置文件等 优点是可 ...

  2. Makefile速查笔记

    Makefile速查笔记 Makefile中的几个调试方法 一. 使用 info/warning/error 增加调试信息 a. $(info "some text")打印 &qu ...

  3. http请求204

    项目中发现一个奇怪的问题,请求的时候同一个接口有两个请求,而且有一个状态为204,有一个为200 在网上查看资料后得知,是因为跨域而引起的,OPTIONS是一种“预检请求”,浏览器在处理跨域访问的请求 ...

  4. js数据类型的转换

    类型转换:就是把一种数据类型转换成另一种数据类型,在js中类型转换有以下三种情况:转数字,转字符串,转布尔值.对象转成这3种数据类型是没有意义的,对象里面可以放任意的数据类型. 在讲解类型转换之前,先 ...

  5. ES6入门一:块级作用域(let&const)、spread展开、rest收集

    let声明 const声明 块级作用域 spread/rest 一.let声明与块作用域 在ES6之前,JavaScript中的作用域基本单元就是function.现在有了let就可以创建任意块的声明 ...

  6. element-ui 表单自定义日期输入校验

    methods: { validateDate(rule, value, callback){ if (value) { let timestamp = new Date(value).getTime ...

  7. Flutter-动画-实践篇

    一.了解AnimatedWidget 通常我们给一个Widget添加动画的时候都需要监听Animation的addListener()方法,并在这个方法里面不停的调用setState()方法通知Wei ...

  8. 密码基础知识(2)以RSA为例说明加密、解密、签名、验签

    密码基础知识(1)https://www.cnblogs.com/xdyixia/p/11528572.html 一.RSA加密简介 RSA加密是一种非对称加密.是由一对密钥来进行加解密的过程,分别称 ...

  9. python之字典一

    字典的定义: 前面我们说过列表,它适合于将值组织到一个结构中并且通过编号对其进行引用.字典则是通过名字来引用值的数据结构,并且把这种数据结构称为映射,字典中的值没有特殊的顺序,都存储在一个特定的键(k ...

  10. axios+post获取并下载后台返回的二进制流

    axios+post获取并下载后台返回的二进制流 let url = $.getCookie('prefixUrl')+'/expenseword/exportWords'; let vm = thi ...