CodeForces 785D Anton and School - 2 (组合数学)
题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’。
析:通过枚举 ‘(’ 来计算有多少种情况,假设 第 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 (组合数学)的更多相关文章
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- 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 ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
- Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...
- Codeforces 785D Anton and School - 2(组合数)
[题目链接] http://codeforces.com/problemset/problem/785/D [题目大意] 给出一个只包含左右括号的串,请你找出这个串中的一些子序列, 要求满足" ...
- CodeForces 785D Anton and School - 2
枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)
大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
随机推荐
- 基于Python语言使用RabbitMQ消息队列(五)
Topics 在前面教程中我们改进了日志系统,相比较于使用fanout类型交易所只能傻瓜一样地广播,我们用direct获得了选择性接收日志的能力. 虽然使用direct类型交易所改进了我们的系统,但它 ...
- Node中没搞明白require和import,你会被坑的很惨
ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...
- C#中将dateTimePicker初始值设置为空
最近在做一个小项目,有一个功能是根据用户选择条件查询数据,要求时间控件的默认值为空,只有当用户修改了时间,才根据时间查询.简单的说,就是默认或者点击清空按钮的情况下,时间控件dateTimePicke ...
- 【转】Cron表达式简介
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- Http之ContentType
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
- Angular5学习笔记 - 配置NG-ZORRO(八)
一.在项目中集成组件 $ cd PROJECT_NAME $ npm install ng-zorro-antd --save 二.在项目中导入组件 直接用下面的代码替换 /src/app/app.m ...
- ORACLE显式授权
同一数据库 两个不同用户 user1 user2 user1里面有一张表 table1 在user2里面创建synonymcreate synonym sy1 for user1.table1; 创建 ...
- 引用 WCF 服务后,没有生成任何 .datasource?
如题WCF服务生成成功,在添加服务引用的时候也不报错,但是添加完成之后不能正常调用服务借口. 在重新引用服务的时候,或者是是更新引用服务的时候,点“高级”按钮,在服务引用设置对话框中,将“重新使用引用 ...
- java代码继承难点。构造方法的调用
总结:子类默认调用父类的无参构造方法.重写时,父类方法将被覆盖,不被调用,在子类中可以使用super.方法():可以实现 运行显示: evente.x:55 evente.x:55 B.y:57 pa ...
- java继承实例基础
总结:多态.重写.构造方法调用 package com.a; public class fsd { int a = 23; public fsd() { System.out.println(4444 ...