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

析:通过枚举 ‘(’ 来计算有多少种情况,假设 第 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. LeetCode Binary Tree Tilt

    原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/ 题目: Given a binary tree, return ...

  2. 我的ubuntu新系统自动装软件脚本

    装一些常用软件 配一下环境变量 #!/bin/bash #download g++sudo apt-get install g++ -y#download codeblockssudo apt-get ...

  3. python3之编码

    这个符号(#!)的名称,叫做"Shebang"或者"Sha-bang"Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件 ...

  4. hihoCoder#1322(树的判定)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶点 M 条边的无向图 G ,判断 G 是不是一棵树. 输入 第一个是一个整数 T ,代表测试数据的组 ...

  5. Spring Boot 集成Swagger2生成RESTful API文档

    Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...

  6. java代码写个进程条

    总结:运用JProgressBar类.还有线程相关 package com.v; import java.awt.image.*; import java.awt.Color; import java ...

  7. Python函数(十)-装饰器(三)

    如果多个函数想通过一个装饰器来实现不同的功能的话,可以给装饰器传入参数,让装饰器里的函数对参数进行判断,来实现不同的功能 # -*- coding:utf-8 -*- __author__ = &qu ...

  8. java 多线程系列基础篇(九)之interrupt()和线程终止方式

    1. interrupt()说明 在介绍终止线程的方式之前,有必要先对interrupt()进行了解.关于interrupt(),java的djk文档描述如下:http://docs.oracle.c ...

  9. 部署和调优 1.3 pureftp部署和优化-2

    登录ftp,用远程的一台机器,执行 lftp 如果没有这个命令,需要安装 yum install -y lftp 登录ftp lftp ftpuser1@192.168.1.117 输入口令,即密码 ...

  10. LookupError: unknown encoding: cp65001解决办法

    一.之前手上做的一个web项目,漏洞频发,服务器用的是菜鸟云服务器,那个应急响应中心不错,想不到乌云倒了,白帽子竟然被阿里系养了,题外话了,首先感谢白帽子提的漏洞,同时也感慨自己安全知识,以及意识的薄 ...