codeforces round 404 div2 D Anton and School - 2 组合数学

探讨了如何高效计算一个括号序列中所有符合特定条件的子序列数量的方法,使用组合数学技巧将复杂度降至O(n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Anton and School - 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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!

Input

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

Output one number — the answer for the task modulo 109 + 7.

Examples
Input
)(()()
Output
6
Input
()()()
Output
7
Input
)))
Output
0
Note

In the first sample the following subsequences are possible:

  • If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())".
  • If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()".
  • If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()".
  • If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()".

The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.




题目描述:给出一个括号的序列,问有多少种方案,使得删去一些字符之后,使得括号序列形成诸如"(((())))"、“(())”这样的先左括号后右括号的序列的形式。


题目思路:枚举每一个左括号出现的位置,假设这个左括号之前有x个左括号(不包括这个左括号),后边有y个右括号,那么以这个左括号为中心括号的合法括号序列一共有

                   ΣC(x , i) * C(y , i + 1)     (0 <= i <= x)个,但这样显然复杂度O(n)没法处理,但是根据组合数知识,有ΣC(x , i) * C(y , i + 1)     (0 <= i <= x)      =     C(x + y , y - 1) (证明

                   方法是比较(1 + t)^x * (1 + t) ^ y 与(1 + t) ^ (x + y)等号左右两边t^(y - 1)的系数),这样预先处理出组合数,然后O(n)枚举每一个位置,把他们的和相加就可以了。

#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxs = 4e5 + 5;
const int maxn = 2e5 + 5;
LL fac[maxs], inv[maxs];
LL q_pow(LL x, LL n)
{
	if (n == 0)      return 1LL;
	LL res = q_pow(x * x % mod, n / 2);
	if (n & 1)      res = res * x % mod;
	return res;
}
void jiecheng()
{
	fac[0] = 1LL;
	for (LL i = 1; i < maxs; i++){
		fac[i] = fac[i - 1] * i % mod;
	}
}
void _inv()
{
	inv[0] = 1;
	for (int i = 1; i < maxs; i++){
		inv[i] = q_pow(fac[i], mod - 2);
	}
}
LL C(int n, int m)
{
	if (n < m)       return 0;
	return  fac[n] * inv[m] % mod * inv[n - m] % mod;
}
char s[maxn];
int main()
{
    jiecheng();
    _inv();
    scanf("%s" , s);
    int len = strlen(s) , rsum = 0;
    for(int i = 0 ; i < len ; i++){
        if(s[i] == ')')
            ++rsum;
    }
    LL ans = 0 , x , y , left = 0 , right = 0;
    for(int i = 0 ; i < len ; i++){
        if(s[i] == '('){
            x = left;
            y = rsum - right;
            ans = (ans + C(x + y , y - 1LL)) % mod;
        }
        if(s[i] == '(')     ++left;
        else                 ++right;
    }
    printf("%lld\n" , ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值