题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’。

析:通过枚举 ‘(’ 来计算有多少种情况,假设 第 i 个括号前面有 n 个 '(',右边有 m 个 ')',那么总共就有 sigma(1, n, C(n-1, i-1)*C(m, i)),其中 1,n 表示从上下限。。

然后这样算的话就是 n 方的复杂度,会超时,再利用范德蒙恒等式(不会的请点击:http://www.cnblogs.com/dwtfukgv/articles/7120297.html)进行化简,可得C(n+m-1, n),

这样就去掉那个求和,复杂度只有 O(n)了,计算组合数时要用逆元。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 300000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL inv[maxn];
LL f[maxn], fact[maxn];
int l[maxn], r[maxn];
char s[maxn]; LL C(int n, int m){
return fact[n] * f[m] % mod * f[n-m] % mod;
} int main(){
f[0] = 1;
inv[1] = f[1] = fact[1] = 1;
for(int i = 2; i < maxn; ++i){
inv[i] = (mod - mod/i) * inv[mod%i] % mod;
f[i] = f[i-1] * inv[i] % mod;
fact[i] = fact[i-1] * i % mod;
}
cin >> s+1;
n = strlen(s+1);
vector<int> v;
for(int i = 1; i <= n; ++i){
l[i] = s[i] == '(' ? l[i-1]+1 : l[i-1];
if(s[i] == '(') v.push_back(i);
}
for(int i = n; i > 0; --i)
r[i] = s[i] == ')' ? r[i+1]+1 : r[i+1];
LL ans = 0;
for(int i = 0; i < v.size(); ++i){
int n = l[v[i]];
int m = r[v[i]];
ans = (ans + C(m+n-1, n)) % mod;
}
cout << ans << endl;
return 0;
}

  

CodeForces 785D Anton and School - 2 (组合数学)的更多相关文章

  1. Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)

    题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...

  2. Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)

    D. Anton and School - 2 time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. [刷题]Codeforces 785D - Anton and School - 2

    Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...

  4. Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]

    题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...

  5. Codeforces 785D Anton and School - 2(组合数)

    [题目链接] http://codeforces.com/problemset/problem/785/D [题目大意] 给出一个只包含左右括号的串,请你找出这个串中的一些子序列, 要求满足" ...

  6. CodeForces 785D Anton and School - 2

    枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...

  7. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  8. Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)

    大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...

  9. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

随机推荐

  1. UGUI transform

    在编辑器中将UGUI对象挂在另一个物体上,UGUI预制体根对象位置调成零,调好位置后保存 由gameframework初始化,再挂到对应父对象上时,位置会有偏差,会更改锚点旋转等信息,需在OnWind ...

  2. SpringCloud微服务实战——第一章序言读书笔记

    什么是微服务架构 是系统架构上的一种设计风格,将独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间基于HTTP的RESTful API进行通信协作. 每个小型服务都围绕各自的 ...

  3. Linux网络编程——I/O复用之poll函数

    一.回顾前面的select select优点: 目前几乎在所有的平台上支持,其良好跨平台支持也是它的一个优点 select缺点: 1.每次调用 select(),都需要把 fd 集合从用户态拷贝到内核 ...

  4. RabbitMQ 四种Exchange

    AMQP协议中的核心思想就是生产者和消费者隔离,生产者从不直接将消息发送给队列.生产者通常不知道是否一个消息会被发送到队列中,只是将消息发送到一个交换机.先由Exchange来接收,然后Exchang ...

  5. 开发环境入门 linux基础 (部分)nginx和nfs

    nginx和nfs 复习总结 rpm -ivh 软件包 --nodeps (没有依赖性安装) rpm -ivh 软件包 --force (覆盖安装) yum -y upgrade 升级所有包,不改变软 ...

  6. c# webapi2 实用详解

    本文介绍webapi的使用知识 发布webapi的问题 配置问题 webapi的项目要前端访问,需要在web.config配置文件中添加如下配置 在system.webServer节点下面添加 < ...

  7. 第十七章 Velocity优化实践(待续)

    现实存在的问题 优化的理论基础 一个高效的模版引擎实现思路 优化成果 其他优化手段

  8. Eclipse中classpath和deploy assembly的文件位置

    classpath的配置信息存储在工程根目录下的.classpath文件 deploy assembly配置信息存储在工程目录下的.settings\org.eclipse.wst.common.co ...

  9. 问题:c# json解析;结果:c# 解析JSON的几种办法

    c# 解析JSON的几种办法 欲成为海洋大师,必知晓海中每一滴水的真名. 刚开始只是想找一个转换JSON数组的方法,结果在MSDN翻到一大把. 搜索过程中免不了碰到一大堆名词:WCF => Da ...

  10. flask 电影系统(2)

    标签,电影,上映预告数据模型设计 标签数据类型 id:编号 name:标题 movies:电影外键关联 addtime:创建时间 定义电影数据模型 id:编号 title:电影标题 url:电影地址 ...