【题目链接】 http://codeforces.com/problemset/problem/785/D

【题目大意】

  给出一个只包含左右括号的串,请你找出这个串中的一些子序列,
  要求满足"(((())))",即左边全是左括号右边全是右括号且数量相等的形式。
  求这样的子序列的数量。

【题解】

  我们枚举每个断点(,计算当这个位置作为最后一个左括号时候的方案数
  设左边有x个左括号,右边有y个右括号,那么统计答案为ΣC(x,i)*C(y,i+1)
  又有C(y,i+1)=C(y,y-1-i),所以答案为ΣC(x,i)*C(y,y-1-i)=C(x+y,y-1)

【代码】

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=400010;
const LL mod=1000000007;
LL f[N],rf[N];
int l[N],r[N],m,n,k;
LL inv(int a,int m){return(a==1?1:inv(m%a,m)*(m-m/a)%m);}
LL C(int n,int m){if(n<m||m<0)return 0;return f[n]*rf[m]%mod*rf[n-m]%mod;}
void init(n){
f[0]=1LL;for(int i=1;i<=n;i++)f[i]=(LL)f[i-1]*i%mod;
rf[n]=inv(f[n],mod);
for(int i=n;i;i--)rf[i-1]=(LL)rf[i]*i%mod;
}
char s[N];
int sl[N],sr[N];
int main(){
init(400000);
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++)sl[i]=sl[i-1]+(s[i]=='(');
for(int i=n;i;i--)sr[i]=sr[i+1]+(s[i]==')');
LL ans=0;
for(int i=1;i<=n;i++)if(s[i]=='('){
int x=sl[i-1],y=sr[i+1];
ans=(ans+C(x+y,y-1))%mod;
}printf("%lld\n",ans);
return 0;
}

Codeforces 785D Anton and School - 2(组合数)的更多相关文章

  1. Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)

    D. Anton and School - 2 time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. [刷题]Codeforces 785D - Anton and School - 2

    Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...

  3. Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]

    题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...

  4. CodeForces 785D Anton and School - 2

    枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...

  5. CodeForces 785D Anton and School - 2 (组合数学)

    题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...

  6. Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)

    题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...

  7. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  8. Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)

    大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...

  9. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

随机推荐

  1. 安卓中使用iconfont

    https://www.cnblogs.com/dongweiq/p/5730212.html

  2. power designer 绘制E-R 图

    总体概括:本篇主要先介绍E-R图的一些基本概念,然后介绍怎么绘制E-R图,特别是用power designer 的反向工程怎么把表中对字段的注释也展示出来. 1.E-R图的基本概念: E-R图就是en ...

  3. io流中的装饰模式对理解io流的重要性

    为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...

  4. Nginx support TCP Load balance

    1. Install nginx package 2. edit nginx configuration file [root@ip- nginx]# more nginx.conf user ngi ...

  5. bzoj 3100 排列

    题目大意: 给你长度为 \(1e6\) 的序列, 求最大的 \(K\) 使得序列中含有一个 \(K\) 的排列 做法: 性质: 区间包含1, 元素不重, 区间最大值=区间长度 枚举一个 \(1\) 让 ...

  6. [object-c 2.0 程序设计]object-c file handle (二)

    // // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...

  7. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  8. 2017年上海金马五校程序设计竞赛:Problem G : One for You (博弈)

    Description Given a m × n chessboard, a stone is put on the top-left corner (1, 1). Kevin and Bob ta ...

  9. bzoj1053 搜索

    2013-11-16 17:43 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1053 因为使pi(prime[i])<20亿的i不 ...

  10. LeetCode 2 :Swap Nodes in Pairs

    我的代码是这样的: class Solution { public: ListNode *swapPairs(ListNode *head) { ; ; ListNode *listA; ListNo ...