题意:给你一串只有‘(’与‘)’的字符串,问你多少对括号,括号一定是左边一半的‘(’,右边一半是‘)’

   )(()()   答案是:6

题解:枚举每个‘(’,此时设左括号左边有n个‘(’,它右边有m个‘)’,当我们设定此时的‘(’一定选定时,就不会重复了

   然后对于每个位置我们就可以推出这样的公式:注意‘)’一定需要一个,且如果n<m则大于m的部分没有意义

   接着我们有范德蒙恒等式:

   我们可以这样理解:在m个人中选择i个,n个人选择k-i个人,则我们可以表示在m+n个人中选择k个人

   接着我们将原来的公式合并:,然后可以将求和上面的n强行变成n+1,最后就可以展开使用阶层与逆元求出

   数据可以分开算,可以合起来计算

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const ll INF=1LL<<;
const double Pi=acos(-1.0);
const ll Mod=1000000007LL;
const int Max=;
char str[Max];
ll fac[Max];//根据阶层求组合数
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)//求逆元
{
if(b==0LL)
{
x=1LL;
y=0LL;
d=a;
}
else
{
exgcd(b,a%b,d,y,x);
y=(y-x*(a/b)%Mod+Mod)%Mod;
}
return;
}
void Init(int n)//初始化阶层
{
fac[]=1LL;
for(int i=;i<n;++i)
{
fac[i]=fac[i-]*i%Mod;
}
return ;
}
ll Jud(ll n,ll m)//组合数公式
{
ll d,x,y,res;
exgcd(fac[n+]*fac[m-]%Mod,Mod,d,x,y);
res=fac[n+m]*((x+Mod)%Mod)%Mod;
return res;
}
int suml[Max],sumr[Max];
ll Solve(int n)
{
ll ans=0LL;
memset(suml,,sizeof(suml));
memset(sumr,,sizeof(sumr));
for(int i=;i<n;++i)//前缀和
{
if(i)
suml[i]=suml[i-];
if(str[i]=='(')
{
suml[i]++;
}
}
for(int i=n-;i>=;--i)//后缀和
{
if(i<n-)
sumr[i]=sumr[i+];
if(str[i]==')')
{
sumr[i]++;
}
}
for(int i=;i<n;++i)
{
if(str[i]=='(')
{
ll n=suml[i]-;//左边有左括号个数
ll m=sumr[i];//右边有右括号个数
if(m)
ans=(ans+Jud(n,m))%Mod;
}
}
return ans;
}
int main()
{
int n;
Init(Max);
while(~scanf("%s",str))
{
n=strlen(str);
printf("%I64d\n",Solve(n));
}
return ;
}

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

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

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

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

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

  3. Codeforces 734E. Anton and Tree 搜索

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

  4. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  5. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  6. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  7. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  8. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  9. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

随机推荐

  1. Android无线测试之—UiAutomator UiDevice API介绍七

    截图与等待空闲 一.截图和等待空闲相关知识: 1)图片缩放比例:例如图片从100像素缩小到50像素 2)图片质量:是指图片大小,质量越高图片越大,质量越低图片越小 3)File类:指的是一个文件或者一 ...

  2. Android自动化测试工具之—UiAutomator环境配置

    1.相关软件下载: 1)JDK: 1.6及其以上版本 2)Eclipse 3)Android SDK 其中Eclipse和Android SDK已经被Google打包成ADT(Android Deve ...

  3. 多个 python的pip版本选择

    如果你电脑里面装了多个版本的python python3 -m pip instatll xlutilspython2 -m pip instatll xlutils 加载新的pippython -m ...

  4. python中json操作

    1.写操作.json文件dumps().dump()函数 d = { 'zll': { 'addr': '北京', 'age': 28 }, 'ljj': { 'addr': '北京', 'age': ...

  5. hdu 3667 (拆边 mcmf)

    注意题目中 边的容量 <= 5.可以把费用权值 a *f ^2化归成 a * f2, 即第一条边费用为 1 * a, 第二条 为 (4 - 1) * a, 第三条为 (9  - 4) * a.. ...

  6. codevs1044 拦截导弹==洛谷 P1020 导弹拦截

    P1020 导弹拦截 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天 ...

  7. Linux 进程间通信(二)(网络IPC:套接字)

    socket描述符 套接字是通信端点的抽象,创建一个套接字使用如下函数: #include <sys/socket.h> int socket(int domain, int type, ...

  8. Axis-Parallel Rectangle

    D - Axis-Parallel Rectangle Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...

  9. 亚马逊订单api重构 api异常入库 在php中执行python

    https://docs.python.org/2/library/xml.etree.elementtree.html python  较 php能更高效第处理xml xpth php  扮演什么角 ...

  10. 内置函数:min 用法

    内置函数:min 用法 源码 def min(*args, key=None): # known special case of min """ min(iterable ...