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

   )(()()   答案是: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. WINDOWS 7.1 SDK 安装失败

    错误提示: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information. 原因:本机上安 ...

  2. 【BZOJ2819】Nim 树状数组+LCA

    [BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...

  3. QThread与多线程(比较清楚)

    QThread类为我们提供了一种平台无关的管理线程的方式.一个QThread对象管理应用程序中的一个线程,该线程从run()函数开始执行.并且,默认情况下,我们可以在run()函数中通过调用QThre ...

  4. Python3.6全栈开发实例[019]

    19.干掉主播.现有如下主播收益信息, 按照要求, 完成相应操作:(1)计算主播平均收益值 sum = 0 for i in zhubo.values(): sum +=i print(round(s ...

  5. 我的Android进阶之旅------>Android横竖屏切换总结

    在默认情况下当屏幕从竖评变到横屏时会触发 onConfigurationChanged 事件 在默认情况下会重新加载画面并显示和横屏一样的画面,这样会有2个问题,   * 布局问题,在竖屏 显示的布局 ...

  6. STL学习笔记— —容器map和multimap

    简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...

  7. sql server监控图解

  8. 001-web基本程序搭建

    一.IDEA创建项目 1.基本项目创建 1.1.基本步骤 1.Create New Project [File→New→Project]→New Project 2.maven→group.artif ...

  9. 标准c内存函数的使用方法

    标准c内存函数 calloc 语法:     #include <stdlib.h>   void *calloc( size_t num, size_t size ); 功能: 函数返回 ...

  10. css position absolute 浮动特性

    absolute的元素不会占据未浮动的元素的空间<html> <head> <style type="text/css"> .flipbox{ ...