CF 785D Anton and School
来源:互联网 发布:js中的li怎么去掉 编辑:程序博客网 时间:2024/04/29 09:24
题意:给你一串字符串(只包含字符'('和')'),求有多少个子串满足:长度是偶数,且左半边只有'(' 右半边只有')',比如"((()))"是一个满足条件的字符串
分析:先记录每个字符左边有多少个'(',右边有多少个')',包含当前字符本身;然后从左往右遍历字符串,如果当前字符是'(',左边(包含本身)有a个'(',右边有b个')',那么满足条件的子字符串就增加了
由最后一个等号左边到等号右边是根据范德蒙恒等式
# include <stdio.h># include <string.h># define MOD 1000000007# define LL long long char s[200005]; int l[200005]={0},r[200005]={0}; LL fac[200005],fac_exp[200005]; LL ModExp(LL a,LL b,LL p)//快速幂取模 a^b%p { LL ans=1; while(b) { if(b&1) ans=ans*a%p; a=a*a%p; b>>=1; } return ans; } LL C(int n,int m) { return fac[n]%MOD * fac_exp[n-m]%MOD * fac_exp[m]%MOD; } int main() { int i,len; LL ans=0; fac[0]=fac_exp[0]=1; for(i=1;i<=200000;i++) { fac[i]=(fac[i-1]*i)%MOD; fac_exp[i]=ModExp(fac[i],MOD-2,MOD); } scanf("%s",s+1); len=strlen(s+1); for(i=1;i<=len;i++) l[i]=(s[i]=='('?l[i-1]+1:l[i-1]); for(i=len;i>=1;i--) r[i]=(s[i]==')'?r[i+1]+1:r[i+1]); for(i=1;i<=len;i++) { if(s[i]=='(') ans=(ans+C(l[i]+r[i]-1,l[i]))%MOD; } printf("%lld",ans); return 0; }
0 0
- CF 785D Anton and School
- CF 785D Anton and School
- codeforces 785 D Anton and School
- codeforces 785D. Anton and School
- codeforces 785 D. Anton and School
- codeforces 785D Anton and School
- CodeForces 785D Anton and School
- Codeforces-785D-Anton and School
- Codeforces 785D Anton and School
- Codeforces 785D Anton and School
- CodeForces 785D Anton and School
- Codeforces785 D. Anton and School
- Codeforces Round #404 D. Anton and School
- Codeforces Round #404 D. Anton and School
- 数学(Anton and School,cf 734F)
- [CF785D]Anton and School
- Codeforces785D. Anton and School
- Codeforces Round #404 (Div. 2) D. Anton and School
- C++的继承(三种继承方式)
- U盘无法显示问题
- 第39级台阶
- tensorflow中张量的理解
- 炉石传说——一中大乱斗
- CF 785D Anton and School
- brew: command not found 修复 mac
- HDU 1495 非常可乐 (BFS)
- 使用指针访问一维数组
- C++中的虚函数、虚函数的作用和使用方法
- acm书中题目 b-2
- 1051
- 归并排序代码
- 传奇程序·m行n图像放大K倍