Anton and School - 2 (组合数学)
题意:给你一串只有‘(’与‘)’的字符串,问你多少对括号,括号一定是左边一半的‘(’,右边一半是‘)’
)(()() 答案是: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 (组合数学)的更多相关文章
- CodeForces 785D Anton and School - 2 (组合数学)
题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
随机推荐
- ubuntu虚拟化平台libvrit-bin
http://download.cirros-cloud.net/0.3.5/ 下载 cirros-0.3.5-x86_64-disk.img 因为 KVM(准确说是 Libvirt)默认不接受远程管 ...
- H - N皇后问题
H - N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- UI auto程序结构组织方式
UI Auto分三个layer: 1. Object finding – 单独一个类,寻找到控件.因为UI auto最容易改动的就是UI界面,这样全部放到一起就便于统一修改. 2. Task - 对控 ...
- java内部类详细介绍
0.内部类与一般类有所不同,它是放在外部类的内部即可作为外部类的成员变量,也可放在方法内部作为局部变量,既然是变量,那么它可以用 private static 修饰符修饰,而外部类则不能,这也是内部类 ...
- Toeplitz matrix
w https://en.wikipedia.org/wiki/Toeplitz_matrix Proof of Stolz-Cesaro theorem | planetmath.org http ...
- Java你不知道的那些事儿—Java隐藏特性
转载自:http://www.cnblogs.com/lanxuezaipiao/p/3460373.html 每 种语言都很强大,不管你是像我一样的初学者还是有过N年项目经验的大神,总会有你不知道的 ...
- SpringBoot处理url中的参数的注解
1.介绍几种如何处理url中的参数的注解 @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是 @RequestMa ...
- python并发编程&多线程(二)
前导理论知识见:python并发编程&多线程(一) 一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 官网链 ...
- Python 开发者节省时间的 10 个小技巧
Python 是一个美丽的语言,可以激发用户对它的爱.所以如果你试图加入程序员行列,或者你有点厌倦C++,Perl,Java 和其他语言,我推荐你尝试Python. Python有很多吸引程序员的功能 ...
- $SublimeText2常用快捷键
1.删除一行:ctrl + shift + K2.替换:ctrl + H3.设置书签:Ctrl+F2设置书签F2 下一个书签Shift+F2上一个书签4.查找:ctrl + F 查找F3 查找下一个s ...