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

析:通过枚举 ‘(’ 来计算有多少种情况,假设 第 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. [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  2. CodeForces 589H Tourist Guide

    传送门 题目大意 给定$n$个点$m$条边的无向图,有$K$个关键点,你要尽可能的让这些关键点两两匹配,使得所有点对之间可以通过简单路径连接且任意两个简单路径没有重复的边(可以是共同经过一个点),输出 ...

  3. think python chapter3

    3.1 built-in function type(42)=> <class 'int'> int('32')=>32 int(3.9) => 3 int(-2.3)= ...

  4. Django基础(四)

    Form表单 Admin     Django Form表单 django 中的form 一般有两种功能: 输入html 验证用户输入 1,先写一个form import re from django ...

  5. String是基本的数据类型吗?

    String不是基本的数据类型,是final修饰的java类,java中的基本类型一共有8个,它们分别为: 1 字符类型:byte,char 2 基本整型:short,int,long 3 浮点型:f ...

  6. c++ 图解快速排序算法

    第一.算法描述 快速排序由C. A. R. Hoare在1962年提出,该算法是目前实践中使用最频繁,实用高效的最好排序算法, 快速排序算法是采用分治思想的算法,算法分三个步骤 从数组中抽出一个元素作 ...

  7. C# Math.Round

    不能直接调用Math.Round方法的,这可和Java的不一样哦Math.Round这个函数的解释是将值按指定的小数位数舍入,并不就是四舍五入.这种舍入有时称为就近舍入或四舍六入五成双 C# code ...

  8. O2O和B2C、C2C的区别

    B2C.C2C是在线支付,购买的商品会塞到箱子里通过物流公司送到你手中;O2O是在线支付,购买线下的商品.服务,再到线下去享受服务. O2O模式的核心很简单,就是把线上的消费者带到现实的商店中去.在线 ...

  9. docker 端口映射iptables: No chain/target/match by that name错误解决方法

    pkill docker iptables -t nat -F ifconfig docker0 down brctl delbr docker0 service docker restart

  10. volatile语义

    volatile在Java内存模型(JMM)中,保证共享变量对所有线程可见,但不保证原子性.volatile语义是同步,通过共享变量的方式,完成线程间的通信. 为什么需要volatile Java内存 ...