Codeforces 785D Anton and School - 2(组合数)
【题目链接】 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(组合数)的更多相关文章
- 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 ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
- Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...
- CodeForces 785D Anton and School - 2
枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...
- CodeForces 785D Anton and School - 2 (组合数学)
题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)
大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
随机推荐
- HDU1828 Picture 线段树+扫描线模板题
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- spring4.3注解
Spring4.3中引进了 {@GetMapping.@PostMapping.@PutMapping.@DeleteMapping.@PatchMapping},分别对应这个查询,插入,更新,删除 ...
- spring结合Mybatis的框架搭建(一)
一:前沿 2015年新年上班的第二天,第一天就打了一天的酱油哦,只是下午开始搭建自己毕业设计的框架,搭建的是spring+spring mvc+MyBatis的框架.今天遇到了一个问题,结果弄了我一天 ...
- Spring MVC框架下 将数据库内容前台页面显示完整版【获取数据库人员参与的事件列表】
1.书写jsp页面包括要显示的内容[people.jsp] <!-- 此处包括三个方面内容: 1.包含 文本输入框 查询按钮 查询结果显示位置 (paging) 2.包括对按钮(button) ...
- sort函数_C++
C++的STL库里有一个 sort 函数,它就是随机化快速排序,速度比快速排序还快,因为它克服了逆序时被卡成O(n2)的情况 想要使用 sort 首先要在头文件里申明 #include<algo ...
- bzoj 1076 状压DP
我们设w[i][s]为当前到第i关,手中的物品为s的时候,期望得分为多少,其中s为二进制表示每种物品是否存在. 那么就比较容易转移了w[i][s]=(w[i-1][s']+v[j]) *(1/k),其 ...
- poj 1528 Perfection
题目链接:http://poj.org/problem?id=1528 题目大意:输入一个数n,然后求出约数的和sum,在与这一个数n进行比较,如果sum>n,则输出ABUNDANT,如果sum ...
- DDCTF - evil 一个伪装成docx的exe
0x01 题目描述 题目比较简单,不过这种题感觉比单纯的逆向算法来有意思的多,所以记录一下~ 0x02 脱壳 先拖到IDA瞅一眼,发现加壳了 用PEID查一下是什么壳,但是没有查出来.使用String ...
- web前端 CSS基础
简单的CSS文件 <style type="text/css"> a{ color:rebeccapurple; font-size: larger; font-wei ...
- swift对比object-c
http://www.cocoachina.com/bbs/read.php?tid=204294 WWDC 2014上苹果再次惊世骇俗的推出了新的编程语言SWIFT( 雨燕 ), 这个消息会前没有半 ...