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 ...
随机推荐
- iPhone程序中的加密处理
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650478 原文链接 : http://www.yifeiyang.ne ...
- 3673: 可持久化并查集 by zky
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2170 Solved: 978[Submit][Status ...
- tomcat 的 Pipeline 机制
一.server.xml 在每个容器对象里面都有一个pipeline,Pipeline就像是每个容器的逻辑总线. <Host name="localhost" appBase ...
- ./bin/console server:run Surprise! There are no commands defined in the "server" namespace.
Let's start the built-in web server: ./bin/console server:run Surprise! There are no commands defi ...
- swift笔记——环境搭建及Hello,Swift!
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/enson16855/article/details/29829601 首先要下载XCode6,仅仅有 ...
- Hexo+yilia添加helper-live2d插件宠物动画,很好玩的哦~~
个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 安装模块: 博客根目录选择cmd命令窗口或者git bash 输入以下代码,安装插件 操作: ...
- 交换机/路由器上的 S口 F口 E口
S口是serial接口的意思,也叫高速异步串口,主要是连接广域网的V.35线缆用的,说白了就是路由器和路由器连接时候用的,可以用命令设置带宽,一般也就在10M.8M左右.F口是FastEthernet ...
- yii2-lock-form 也许这就是你想要的,阻止表单多次提交
是不是被用户的行为所困扰? 一个表单用户点击提交按钮了N次,这也导致了数据提交了N次. 为了此受到了测试的欺辱,受到了老板的批评? 不用怕,它就是来拯救你的. 第一步:打开命令行,敲入 compose ...
- VMware安装Centos7过程
VMware安装Centos7过程 1.打开VMwear选择新建虚拟机 2.典型安装与自定义安装 典型安装:VMwear会将主流的配置应用在虚拟机的操作系统上,对于新手来很友好. 自定义安装:自定义安 ...
- perspective 能玩点什么
今天看又在看张鑫旭的博客,本来是在玩 transform:Matrix() 的,有讲到单个变化的矩阵设置,但多个变化的就不是那么回事了. 不过这都不是事啦,人生嘛,显然总会有些难关不是轻易能过去的,反 ...