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

   )(()()   答案是:6

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

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

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

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

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

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

  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<vector>
  7. #include<string>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iomanip>
  11. #include<stdlib.h>
  12. #include<iostream>
  13. #include<algorithm>
  14. using namespace std;
  15. #define eps 1E-8
  16. /*注意可能会有输出-0.000*/
  17. #define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
  18. #define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
  19. #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
  20. #define mul(a,b) (a<<b)
  21. #define dir(a,b) (a>>b)
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. const int Inf=<<;
  25. const ll INF=1LL<<;
  26. const double Pi=acos(-1.0);
  27. const ll Mod=1000000007LL;
  28. const int Max=;
  29. char str[Max];
  30. ll fac[Max];//根据阶层求组合数
  31. void exgcd(ll a,ll b,ll &d,ll &x,ll &y)//求逆元
  32. {
  33. if(b==0LL)
  34. {
  35. x=1LL;
  36. y=0LL;
  37. d=a;
  38. }
  39. else
  40. {
  41. exgcd(b,a%b,d,y,x);
  42. y=(y-x*(a/b)%Mod+Mod)%Mod;
  43. }
  44. return;
  45. }
  46. void Init(int n)//初始化阶层
  47. {
  48. fac[]=1LL;
  49. for(int i=;i<n;++i)
  50. {
  51. fac[i]=fac[i-]*i%Mod;
  52. }
  53. return ;
  54. }
  55. ll Jud(ll n,ll m)//组合数公式
  56. {
  57. ll d,x,y,res;
  58. exgcd(fac[n+]*fac[m-]%Mod,Mod,d,x,y);
  59. res=fac[n+m]*((x+Mod)%Mod)%Mod;
  60. return res;
  61. }
  62. int suml[Max],sumr[Max];
  63. ll Solve(int n)
  64. {
  65. ll ans=0LL;
  66. memset(suml,,sizeof(suml));
  67. memset(sumr,,sizeof(sumr));
  68. for(int i=;i<n;++i)//前缀和
  69. {
  70. if(i)
  71. suml[i]=suml[i-];
  72. if(str[i]=='(')
  73. {
  74. suml[i]++;
  75. }
  76. }
  77. for(int i=n-;i>=;--i)//后缀和
  78. {
  79. if(i<n-)
  80. sumr[i]=sumr[i+];
  81. if(str[i]==')')
  82. {
  83. sumr[i]++;
  84. }
  85. }
  86. for(int i=;i<n;++i)
  87. {
  88. if(str[i]=='(')
  89. {
  90. ll n=suml[i]-;//左边有左括号个数
  91. ll m=sumr[i];//右边有右括号个数
  92. if(m)
  93. ans=(ans+Jud(n,m))%Mod;
  94. }
  95. }
  96. return ans;
  97. }
  98. int main()
  99. {
  100. int n;
  101. Init(Max);
  102. while(~scanf("%s",str))
  103. {
  104. n=strlen(str);
  105. printf("%I64d\n",Solve(n));
  106. }
  107. return ;
  108. }

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. iPhone程序中的加密处理

    本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650478     原文链接 : http://www.yifeiyang.ne ...

  2. 3673: 可持久化并查集 by zky

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 2170  Solved: 978[Submit][Status ...

  3. tomcat 的 Pipeline 机制

    一.server.xml 在每个容器对象里面都有一个pipeline,Pipeline就像是每个容器的逻辑总线. <Host name="localhost" appBase ...

  4. ./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 ...

  5. swift笔记——环境搭建及Hello,Swift!

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/enson16855/article/details/29829601 首先要下载XCode6,仅仅有 ...

  6. Hexo+yilia添加helper-live2d插件宠物动画,很好玩的哦~~

    个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 安装模块: 博客根目录选择cmd命令窗口或者git bash 输入以下代码,安装插件 操作: ...

  7. 交换机/路由器上的 S口 F口 E口

    S口是serial接口的意思,也叫高速异步串口,主要是连接广域网的V.35线缆用的,说白了就是路由器和路由器连接时候用的,可以用命令设置带宽,一般也就在10M.8M左右.F口是FastEthernet ...

  8. yii2-lock-form 也许这就是你想要的,阻止表单多次提交

    是不是被用户的行为所困扰? 一个表单用户点击提交按钮了N次,这也导致了数据提交了N次. 为了此受到了测试的欺辱,受到了老板的批评? 不用怕,它就是来拯救你的. 第一步:打开命令行,敲入 compose ...

  9. VMware安装Centos7过程

    VMware安装Centos7过程 1.打开VMwear选择新建虚拟机 2.典型安装与自定义安装 典型安装:VMwear会将主流的配置应用在虚拟机的操作系统上,对于新手来很友好. 自定义安装:自定义安 ...

  10. perspective 能玩点什么

    今天看又在看张鑫旭的博客,本来是在玩 transform:Matrix() 的,有讲到单个变化的矩阵设置,但多个变化的就不是那么回事了. 不过这都不是事啦,人生嘛,显然总会有些难关不是轻易能过去的,反 ...