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

题意:给你一个只包含'('和')'的字符串,然后问他的子序列中有多少满足前一半是左括号,后一半是右括号。

分析:看到题就想到是组合数学,对于一个左括号,他能影响到的右括号就是他后边的,因此,你需要求前缀和和后缀和,来这样来求组合数。现在我们枚举左括号,当枚举到他时,代表他一定被选,前缀和为n,后缀和为m,然后在他前边选i=0到min(n,m)-1个,在他后边选前边数+1个。然后就是C(n-1,i)*C(m,i+1) ,也就是C(n-1,i)+C(m,m-i-1);求和即为C(n+m-1,m-1),然后用卢卡斯定理求即可,注意存n!还有求逆元。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 long long mod=1e9+7;
 5 char s[200005];
 6 int a[200005];
 7 int b[200005];
 8 long long num[200005];
 9 long long qpow(long long x,long long n){
10     if(n==0) return 1;
11     long long ans=1;
12     x=x%mod;
13     while(n!=0){
14         if(n&1) {
15             ans=ans*x%mod;
16         }
17         x=x*x%mod;
18         n=n/2;
19     }
20     return ans;
21 }
22 long long Cn(long long n,long long m){
23     if(n<m) return 0;
24     if(n==m) return 1;
25     if(m>n-m) m=n-m;
26
27     return (num[n]*qpow(num[n-m]*num[m],mod-2))%mod;
28 }
29
30 int main(){
31     ios_base::sync_with_stdio(0);
32     cin.tie(0);
33     num[0]=1;
34     for(long long i=1;i<=200000;i++){
35         num[i]=num[i-1]*i%mod;
36     }
37     cin>>s;
38     int d=strlen(s);
39     int num1=0,num2=0;
40     for(int i=0;i<d;i++){
41         if(s[i]=='('){
42             num1++;
43         }
44         a[i]=num1;
45     }
46     for(int i=d-1;i>=0;i--){
47         if(s[i]==')'){
48             num2++;
49         }
50         b[i]=num2;
51     }
52     long long result=0;
53     for(int i=0;i<d;i++){
54         if(s[i]=='('){
55            result=(result+Cn(a[i]+b[i]-1,b[i]-1))%mod;
56         //cout<<result<<endl;
57         }
58     }
59     cout<<result<<endl;
60     return 0;
61 }

View Code

转载于:https://www.cnblogs.com/ls961006/p/6947669.html

codeforces 785D D. Anton and School - 2相关推荐

  1. Codeforces 785 D.Anton and School - 2(组合数处理)

    Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...

  2. Anton and School - 2 CodeForces - 785D 范德蒙恒等式

    题目传送门:Problem - 785D - Codeforces As you probably know, Anton goes to school. One of the school subj ...

  3. CodeForces - 1526D Kill Anton(模拟)

    题目链接:https://vjudge.net/problem/CodeForces-1526D 题目大意:给出一个只有四种字母组成的字符串 AAA,要求将其重排列 BBB,使得贡献最大.贡献指的是, ...

  4. codeforces 324# E. Anton and Ira (暴力枚举+贪心)

    题目:http://codeforces.com/contest/584/problem/E 题意:给定两个排列p1和p2.可以交换p1中的两个元素p1[i],p1[j],花费为|i-j|,求最小的话 ...

  5. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  6. 【CodeForces - 508B】Anton and currency you all know (思维贪心)

    题干: Berland, 2016. The exchange rate of currency you all know against the burle has increased so muc ...

  7. codeforces 584 E. Anton and Ira(贪心,数组p经变换到数组s)

    题目:http://codeforces.com/contest/584/problem/E 题意:有两个数组p,s,长度为n,且是1~n的排列.要使数组p变为s,每交换 i 和 j 两个位子上的数, ...

  8. 51nod 1574 || Codeforces 584 E. Anton and Ira 思维+构造+贪心

    传送门:E. Anton and Ira 题意:给定两个1-n的全排列p和s,假设交换pi和pj的代价是abs(i-j),问怎样将p交换成s才能使代价最小. 思路:先将s映射成1-n的顺序排列,再将p ...

  9. 【Codeforces 785D】范德蒙恒等式

    1.题目链接.题目大意:一个由括号构成的序列,让你找到这个序列中有多少个好的子序列.一个好的子序列的定义是这样的: 2.分析:这个题是一个组合数学问题.对于每一个'(',我们统计它的左边有多少个和它相 ...

最新文章

  1. Java NIO中的Buffer
  2. javascript 基础篇2 数据类型,语句,函数
  3. Python 代码转 Latex 公式,这个开源库用一行代码帮你搞定
  4. ALDownloadManager 基于Alamofire封装的下载器
  5. MySQL各种类型实验
  6. P4884-多少个1?【BSGS】
  7. ddl hibernate_Hibernate:DDL模式生成
  8. 修改服务器时间报错,修改服务器时间linux
  9. 这个超时问题花了我两周才解决!
  10. CSS:理解:Before和:After伪元素
  11. MTCNN可攻破?华为提出一种可复制、可靠的攻击方法
  12. struts1.x 标签库
  13. JavaScript 教程
  14. 2019年2月CNNIC第43次《中国互联网络发展状况统计报告》
  15. PICkits3调试功能
  16. Python中文字符串,变成英文字符串
  17. DB2查看事务日志使用空间
  18. Map应用:电话聊天狂人
  19. 手机便签软件哪个好用?哪种手机便签软件好使用
  20. 第三届光明区创新创业大赛——聚各方力量,助光明璀璨

热门文章

  1. zabbix登山路——简单监控_各项参数解析
  2. VC2010 编译问题
  3. forward declaration of class 错误
  4. 修改中断向量ESC退出
  5. 封装、继承、多态、访问符,范围修饰符
  6. 本科理工男如何学习Linux
  7. java面试基础问题积累----多线程,并发
  8. [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.5
  9. 如何把字符串类型转换成整型?
  10. AutoCAD2012打开后一闪的解决方法