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. VMware 12安装虚拟机Mac OS X 10.10使用小技巧(虚拟机Mac OS X 10.10时间设置,虚拟机Mac OS X 10.10通过代理上网,Mac OS X 10.10虚拟机优化,VMware虚拟机相互复制)

    1:修改Mac OS 系统时间 2:Mac OS系统 通过代理上网 VMware 12安装Mac OS X 10.10虚拟机优化心得 虚拟显卡硬伤,所以必须要优化下才能用,优化的原则就是能精简的精简, ...

  2. O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)

    题目要求:定义栈的数据结构,添加min().max()函数(动态获取当前状态栈中的最小元素.最大元素),要求push().pop().min().max()的时间复杂度都是O(1). 思路解析:根据栈 ...

  3. Part2-HttpClient官方教程-Chapter2-连接管理

    2.1 连接持久性 建立从一个主机到另一个主机的连接的过程相当复杂,并且涉及两个端点之间的多个分组交换,这可能相当耗时.连接握手的开销可能很大,特别是对于小型的HTTP消息. 如果可以重新使用开放连接 ...

  4. jQuery Validate插件 验证实例

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation Validate手册: http://www.cnblogs.co ...

  5. selenium===selenium自动化添加日志(转)

    本文转自 selenium自动化添加日志 于logging日志的介绍,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 先封装logging模块,保存到common文件夹命名为logger.p ...

  6. python基础===open()文件处理使用介绍

    本文转自:Python open()文件处理使用介绍 1. open()语法open(file[, mode[, buffering[, encoding[, errors[, newline[, c ...

  7. 无缝滚动Js

    <html> <body> <div style="width: 190px; height: 127px; overflow: hidden; font-si ...

  8. c语言中数组,指针数组,数组指针,二维数组指针

    1.数组和指针 ] = {,,,,};// 定义数组 // 1. 指针和数组的关系 int * pa = array; pa = array; // p[0] == *(p+0) == array[0 ...

  9. java的IO流之字符流

    # 原创,转载请留言联系 输出流 FileWriter类 常见的构造方法: FileWriter(String fileName)     根据给定的文件名构造一个 FileWriter 对象.Fil ...

  10. 安装ubuntu-server16.0,设置WiFi

    想装个server版的Linux系统玩玩,下面记录一下遇到的坑. 1:安装语言选英文:可能是因为其他原因,我选中文的时候安装失败了,最后一次选中文的时候成功了. 2:以前装了一个ubuntu的,后面想 ...