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. 【Android XML】Android XML 转 Java Code 系列之 介绍(1)

    最近在公司做一个项目,需要把Android界面打包进jar包给客户使用.对绝大部分开发者来说,Android界面的布局以XML文件为主,并辅以少量Java代码进行动态调整.而打包进jar包的代码,意味 ...

  2. 判断cookie创建的时间是否已经24小时

    def read_cookie(self): cookiesfilepath="cookies%s" % self.uid if os.path.exists(cookiesfil ...

  3. 设计模式之笔记--适配器模式(Adapter)

    适配器模式(Adapter) 定义 适配器模式(Adapter),将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 适配器模式 ...

  4. Scrapy爬虫:抓取大量斗图网站最新表情图片

      一:目标 第一次使用Scrapy框架遇到很多坑,坚持去搜索,修改代码就可以解决问题.这次爬取的是一个斗图网站的最新表情图片www.doutula.com/photo/list,练习使用Scrapy ...

  5. 获取windows 网卡GUID和ip信息

    # coding: UTF-8 import _winreg GUID=dict() num = 0 netCfgInstanceID = None hkey = _winreg.OpenKey(_w ...

  6. tornado样板

    python tornado  样版 (包含出错页面) 2018-02-27  13:07:30 1 # -*- coding:utf-8 -*- 2 3 import tornado.web 4 i ...

  7. linux命令(50):top命令

    TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...

  8. request.getRemoteAddr() 获取的值为0:0:0:0:0:0:0:1的原因及解决办法

    问题: 在近期开发的javaweb项目中,需要记录登录时的电脑ip地址和主机名,通过request.getRemoteAddr()和request.getRemoteHost()得到的值都是0:0:0 ...

  9. hdu 1829(继续扩展并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. maven项目的pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 " 2 xmlns:xsi="http://www.w3.org ...