codeforces 785D D. Anton and School - 2
题目链接: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相关推荐
- Codeforces 785 D.Anton and School - 2(组合数处理)
Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...
- Anton and School - 2 CodeForces - 785D 范德蒙恒等式
题目传送门:Problem - 785D - Codeforces As you probably know, Anton goes to school. One of the school subj ...
- CodeForces - 1526D Kill Anton(模拟)
题目链接:https://vjudge.net/problem/CodeForces-1526D 题目大意:给出一个只有四种字母组成的字符串 AAA,要求将其重排列 BBB,使得贡献最大.贡献指的是, ...
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
题目:http://codeforces.com/contest/584/problem/E 题意:给定两个排列p1和p2.可以交换p1中的两个元素p1[i],p1[j],花费为|i-j|,求最小的话 ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【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 ...
- codeforces 584 E. Anton and Ira(贪心,数组p经变换到数组s)
题目:http://codeforces.com/contest/584/problem/E 题意:有两个数组p,s,长度为n,且是1~n的排列.要使数组p变为s,每交换 i 和 j 两个位子上的数, ...
- 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 ...
- 【Codeforces 785D】范德蒙恒等式
1.题目链接.题目大意:一个由括号构成的序列,让你找到这个序列中有多少个好的子序列.一个好的子序列的定义是这样的: 2.分析:这个题是一个组合数学问题.对于每一个'(',我们统计它的左边有多少个和它相 ...
最新文章
- Java NIO中的Buffer
- javascript 基础篇2 数据类型,语句,函数
- Python 代码转 Latex 公式,这个开源库用一行代码帮你搞定
- ALDownloadManager 基于Alamofire封装的下载器
- MySQL各种类型实验
- P4884-多少个1?【BSGS】
- ddl hibernate_Hibernate:DDL模式生成
- 修改服务器时间报错,修改服务器时间linux
- 这个超时问题花了我两周才解决!
- CSS:理解:Before和:After伪元素
- MTCNN可攻破?华为提出一种可复制、可靠的攻击方法
- struts1.x 标签库
- JavaScript 教程
- 2019年2月CNNIC第43次《中国互联网络发展状况统计报告》
- PICkits3调试功能
- Python中文字符串,变成英文字符串
- DB2查看事务日志使用空间
- Map应用:电话聊天狂人
- 手机便签软件哪个好用?哪种手机便签软件好使用
- 第三届光明区创新创业大赛——聚各方力量,助光明璀璨