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. python中的argparse模块

    argparse干什么用的? 答:参数设置,比如python demo.py -h 诸如此类的. 开始学习这个模块: parser = argparse.ArgumentParser() #使用这个模 ...

  2. linux pthread【转】

    转自:http://www.cnblogs.com/alanhu/articles/4748943.html Posix线程编程指南(1) 内容:  一. 线程创建  二.线程取消 关于作者  线程创 ...

  3. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff 思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角 ...

  4. squid 代理服务

    squid代理服务分为两种方式: 一.正向代理(用在企业的办公环境中,员工上网需要通过Squid代理来上网) 客户端发送请求到代理服务器,代理服务器去向真正的服务器请求结果,并将结果返回给客户端 二. ...

  5. CentOS 7下安装php-redis扩展及简单使用

    前言: 在本篇文章中,我将给大家介绍如何在CentOS7上安装PHP-Redis扩展以及一些简单的实用,关于如何在Centos上安装redis的,可以参考 Redis在CentOS 7上的安装部署   ...

  6. linux的rpm教程

    1.rmp查询 1.1 软件包详细信息 rpm -qpi  httpd-2.4.25-9.fc27.x86_64.rpm 系统将会列出这个软件包的详细资料,包括含有多少个文件.各文件名称.文件大小.创 ...

  7. hdu多校4

    Problem L. Graph Theory Homework 思路:很容易想到一步从 1 走到 n 最优. #include<bits/stdc++.h> #define LL lon ...

  8. 在 Ubuntu 系统安装 Redi

    在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 Redis $ redi ...

  9. python 截取指定长度汉字

    这个方法不是很好,不知道有没有更好的方法 def cut_hz(s, length): charstyle = chardet.detect(s) t = s[:length] try: unicod ...

  10. 【转载】文件下载FileDownloader

    原文地址:https://github.com/lingochamp/FileDownloader 特点 简单易用 高并发 灵活 可选择性支持: 独立/非独立进程 自动断点续传 需要注意 当下载的文件 ...