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 ...
随机推荐
- 【BZOJ1977】[BeiJing2010组队]次小生成树 Tree 最小生成树+倍增
[BZOJ1977][BeiJing2010组队]次小生成树 Tree Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C ...
- (八)solr7实现搜索框的自动提示并统计词频
solr7实现搜索框的自动提示并统计词频 1:用solr 的suggest组件,统计词频相对麻烦. 2:用TermsComponent,自带词频统计功能. Terms组件提供访问索引项的字段和每个词 ...
- WPF中DPI的问题
先搞清楚一下几个概念: DPI:dots per inch ,每英寸的点数.我们常说的鼠标DPI,是指鼠标移动一英寸的距离滑过的点数:打印DPI,每英寸的长度打印的点数:扫描DPI,每英寸扫描了多 ...
- css,查询相应标签,div等
1.类名 .类别 例子: 查询类名为“useradd” .useradd{ margin-top:50px; margin-left:200px;} 2.属性找 例子:查询类为useradd下的inp ...
- Truthy Falsy
https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy falsy(虚值)是在 Boolean 上下文中已认定可转换为‘假‘的值. JavaS ...
- 使用QtConcurrent编写多线程程序(也可以阻塞)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Amnes1a/article/details/66470751Qt在其QtConcurrent命名空 ...
- jquery生成二维码图片
1.插件介绍 jquery.qrcode.min.js插件是jq系列的基于jq,在引入该插件之前要先引入jq.能够在客户端生成矩阵二维码QRCode 的jquery插件 ,使用它可以很方便的在页面上生 ...
- python函数回顾:abs()
函数:abs() 官方英文文档解释 abs(x) Return the absolute value of a number. The argument may be a plain or long ...
- 关于Java中的toString()方法
package c07; class ewq{ public String toString() { return "ppppppppp"; } public static voi ...
- 使用Kotlin开发Android应用(II):创建新工程
在基本了解什么是Kotlin以及Kotlin可以做什么之后,接下来就到了配置Android Studio并使用Kotlin开发Android apps的时候了.首次配置Android Studio需要 ...