Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)
2 seconds
256 megabytes
standard input
standard output
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)).
On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met:
- It is not empty (that is n ≠ 0).
- The length of the sequence is even.
- First
charactes of the sequence are equal to "(".
- Last
charactes of the sequence are equal to ")".
For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS.
Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted.
Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 109 + 7.
Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it!
The only line of the input contains a string s — the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000.
Output one number — the answer for the task modulo 109 + 7.
)(()()
6
()()()
7
)))
0
题目链接:CF 785D
这道题实际上就算不能过也可以是可以写一下的,就是基本会TLE……记当前位置为x,[1,x]中的左括号个数为L,[x+1,len]中右括号个数为R,可以发现每次增加一个 '(',可以跟右边组合的情况多了$$\sum_{i=0}^{min(L-1,R-1)}\binom{L-1}{i} * \binom{R}{i+1}$$
这个式子把右边的组合数又可以化成$$\sum_{i=0}^{min(L-1,R-1)}\binom{L-1}{i} * \binom{R}{R-i-1}$$,可以发现下面之和是一个常数即$R-1$,这个时候就出现了很厉害的公式——范德蒙恒等式
然后就不用每一次都for一遍把组合数加起来,而是加上组合数$$\binom{L-1+R}{R-1} $$就行。
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 200010;
const LL MOD = 1e9 + 7;
char s[N];
LL fac[N], inv[N];
int preL[N], preR[N]; LL qpow(LL a, LL b, LL m)
{
LL r = 1LL;
while (b)
{
if (b & 1)
r = r * a % m;
a = a * a % m;
b >>= 1;
}
return r;
}
void init()
{
fac[0] = 1LL;
inv[0] = 1LL;
for (LL i = 1; i < N; ++i)
{
fac[i] = fac[i - 1] * i % MOD;
inv[i] = qpow(fac[i], MOD - 2, MOD);
}
}
LL combine(LL n, LL m, LL mod)
{
LL ret = ((fac[n] * inv[m]) % mod * inv[n - m]) % mod;
return ret;
}
int main(void)
{
init();
int i;
while (~scanf("%s", s + 1))
{
CLR(preL, 0);
CLR(preR, 0);
int len = strlen(s + 1);
for (i = 1; i <= len; ++i)
{
preL[i] = preL[i - 1] + (s[i] == '(');
preR[i] = preR[i - 1] + (s[i] == ')');
}
LL ans = 0LL;
for (i = 1; i <= len; ++i)
{
if (s[i] == '(')
{
int rightR = preR[len] - preR[i];
ans = ans + combine(preL[i] - 1 + rightR, rightR - 1, MOD);
if (ans > MOD)
ans %= MOD;
}
}
printf("%I64d\n", ans);
}
return 0;
}
Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)的更多相关文章
- Codeforces 785D Anton and School - 2(组合数)
[题目链接] http://codeforces.com/problemset/problem/785/D [题目大意] 给出一个只包含左右括号的串,请你找出这个串中的一些子序列, 要求满足" ...
- [刷题]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
枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...
- CodeForces 785D Anton and School - 2 (组合数学)
题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Outp ...
- 【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 ...
随机推荐
- 问题 F: 等比数列
问题 F: 等比数列 时间限制: 1 Sec 内存限制: 64 MB提交: 2699 解决: 1214[提交][状态][讨论版][命题人:外部导入] 题目描述 已知q与n,求等比数列之和: 1+q ...
- python_37_文件修改
f=open('yesterday','r',encoding='utf-8') f_new=open('yesterday_update','w',encoding='utf-8') for lin ...
- 【SQL】连接 —— 内连接、外连接、左连接、右连接、交叉连接
连接 · 内连接 · 外连接 · 左连接 · 右连接 · 全连接 · 交叉连接 · 匹配符号(+) 连接 根据表之间的关系,呈现跨表查询的结果. 外连接 内连接 左连接 右连接 全 ...
- 四、Shell 数组
Shell 数组 数组中可以存放多个值.Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似). 与大部分编程语言类似,数组元素的下标由0开始. She ...
- 最新Python3.6从入门到高级进阶实战视频教程
点击了解更多Python课程>>> 最新Python3.6从入门到高级进阶实战视频教程 第1篇 Python入门导学 第2篇 Python环境装置 第3篇 了解什么是写代码与Pyth ...
- JZOJ 3470. 【NOIP2013模拟联考8】最短路(path)
470. [NOIP2013模拟联考8]最短路(path) (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- JZOJ 3463. 【NOIP2013模拟联考5】军训
3463. [NOIP2013模拟联考5]军训(training) (Standard IO) Time Limits: 2000 ms Memory Limits: 262144 KB Deta ...
- 01.VUE学习一
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- Linux命令之---ls
命令简介: ls(list)命令用来列出目标目录(缺省的话为当前目录)中所有的子目录和文件. 指令所在路径:/bin/ls 执行权限:All User 命令语法: ls [选项] [目录名] 命令参数 ...