http://codeforces.com/blog/entry/50996

官方题解讲得很明白,在这里我复述一下。

枚举每个左括号,考虑计算一定包含其的简单括号序列的个数,只考虑其及其左侧的左括号,以及其右侧的右括号。最后答案就是其之和。

可以将其提取出来这样((((((())),红色为当前左括号。设有x个左,y个右

要注意,这个答案为C(x+y-1,x),来证明。

我们只需证明,这个答案与长度为x+y-1的,包含x个1的零一序列的种类数相等即可。

随便写一个这样的零一序列,长度为x+y,但当前的左括号的位置一定是零。

比如110100111,除了红色位置以外,设有z个右括号对应1,那么有(x-z)个左括号对应1,那么有(x-1-(x-z))=z-1个左括号对应0。

恰好枚举遍了包含1,2,...,y个右括号,0,1,...,y-1个左括号(不含当前位置)的所有情况。于是显然这个答案是对的。

具体算的时候,预处理一下阶乘,暴力搞一下逆元就行了。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
#define MOD 1000000007ll
ll Quick_Pow(ll a,ll p)
{
if(!p){
return 1;
}
ll ans=Quick_Pow(a,p>>1);
ans=ans*ans%MOD;
if((p&1)==1){
ans=ans*a%MOD;
}
return ans;
}
int n;
char a[200010];
ll jc[200010],ans,sumr;
int main(){
// freopen("d.in","r",stdin);
scanf("%s",a+1);
n=strlen(a+1);
jc[0]=1ll;
for(int i=1;i<=n;++i){
jc[i]=(jc[i-1]*(ll)i)%MOD;
}
for(int i=1;i<=n;++i){
sumr+=(a[i]==')');
}
int x=0,y=0;
for(int i=1;i<=n;++i){
if(a[i]=='('){
++x;
}
else{
++y;
}
if(sumr-y>=1 && a[i]=='('){
ans=(ans+(((jc[x+sumr-y-1]*Quick_Pow(jc[x],MOD-2ll))%MOD)*Quick_Pow(jc[sumr-y-1],MOD-2ll))%MOD)%MOD;
}
}
cout<<ans<<endl;
return 0;
}

【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2的更多相关文章

  1. Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学

    D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...

  2. Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分

    C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...

  3. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  4. Codeforces Round #404 (Div. 2) A - Anton and Polyhedrons 水题

    A - Anton and Polyhedrons 题目连接: http://codeforces.com/contest/785/problem/A Description Anton's favo ...

  5. Codeforces Round #404 (Div. 2) D. Anton and School - 2

    题目链接 转自 给你一个字符串问你能构造多少RSBS. #include<bits/stdc++.h> #define LL long long #define fi first #def ...

  6. 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale

    当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...

  7. Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)

    E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...

  8. Codeforces Round #404 (Div. 2) C 二分查找

    Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18)  找到 1) [n<= m] cout<<n; 2) ...

  9. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

随机推荐

  1. 利用itext将html转为pdf

    亲测代码没有问题,需要注意细节已经标注:需要jar包:iText-2.0.8.jar:core-renderer-R8.jar: core-renderer-R8.jar下载地址:http://cen ...

  2. python设计模式之单例模式(二)

    上次我们简单了解了一下什么是单例模式,今天我们继续探究.上次的内容点这 python设计模式之单例模式(一) 上次们讨论的是GoF的单例设计模式,该模式是指:一个类有且只有一个对象.通常我们需要的是让 ...

  3. HDU 5117 Fluorescent

    题目链接:HDU-5117 题意为有n盏灯,m个开关,每个开关控制着\( k_{i} \)灯.X为最后亮着的灯的个数,要求出\( E(X^{3} ) * 2^{M} mod (10^9 + 7) \) ...

  4. MD5加密学习

    MD5(Message Digest --消息摘要算法)算法是一种散列(hash)算法(摘要算法,指纹算法),不是一种加密算法(易错),任何长度的任意内容都可以用MD5计算出散列值.主要作用是[验明“ ...

  5. [路由] -- Yii2 url地址美化与重写

    转载:http://blog.csdn.net/lmjy102/article/details/53857520

  6. css3属性书写的时候带的一些前缀的意思

    使用css3属性时,大部分都要带这些识别前缀,早期点的浏览器才能识别.现在最新版的浏览器基本都支持css3 基本都不用写前缀 ,写前缀是为了向前兼容老版本的浏览器而已. -ms-transform: ...

  7. java中的数组与集合相互转换

    1.数组转换成集合 数组转换为集合,用Arrays.asList方法. public static void main(String[] args) { String[] arr = {"a ...

  8. django使用haystack对接Elasticsearch实现商品搜索

    # 原创,转载请留言联系 前言: 在做一个商城项目的时候,需要实现商品搜索功能. 说到搜索,第一时间想到的是数据库的 select * from tb_sku where name like %苹果手 ...

  9. git报错The authenticity of host 'github.com (13.229.188.59)' can't be established. RSA key fingerprint is。。。

    额,记录下,.ssh/rsa_pub的内容都加到githup.com配置来,为什么还是报这个错呢. 最后发现是个小白问题,如下图 只需要输入yes就ok.

  10. vue2.0使用watch监听对象属性

    二话不说直接代码,找了一个百度都没找到.... var head=new Vue({ data:{ checkBoxState:{//监听设置开关勾选状态 notice:true, sound:tru ...