Codeforces Round #404 (Div. 2) D. Anton and School - 2


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 123 and 4, we will get the subsequence "()".
  • If we delete characters at the positions 124 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 125 and 6, we will get the subsequence "()".
  • If we delete characters at the positions 134 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 135 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.


题意:给你一个字符串由'('和')'组成,问你有多少个子串,前半部分是由'('组成后半部分由')'组成。

思路:首先很容易能想到要统计每一个位置的前面有几个'('和后面有几个')',我们先分别用n和m来表示吧。然后我们枚举每个位置,如果当前位置是'('那么答案应该增加sigmaC(n-1,i)+C(m,i+1),可能有人会看不懂,我先解释一下,因为每扫到一个位置是'(',那么增加的部分一定拥有这个位置的'(',所以左边n要-1,右边i要+1。然后这里如果每一个位置都这样求的话,复杂度是O(n²),很明显不行。所以剩下的就是化简组合数了。。讲道理我组合数这么渣还玩这个。。。我们来看下,右边的C(m,i+1)等价于C(m,m-i-1),那么原式=sigmaC(n-1,i)+C(m,m-i-1),我们可以发现取数的和是个定值,为m-1,固等价于sigmaC(n+m-1,m-1),然后再套逆元,这道题就解决了。。。好恶心的题,下面给代码:

#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<map>
#define maxn 200005  
#define inf 0x3f3f3f3f  
using namespace std; 
typedef long long LL; 
const double eps=1e-8;
#define N 200005
const LL mod=1e9+7;
LL fac[N];  
char s[N];
int p,lf[N],rg[N];
void init()  
{  
    int i;  
    fac[0] =1;  
    for(i =1; i <= p; i++)  
        fac[i] = fac[i-1]*i%mod;  
}  
LL pow(LL a, LL b)  
{  
    LL tmp = a % mod, ans =1;  
    while(b)  
    {  
        if(b &1)  ans = ans * tmp % mod;  
        tmp = tmp*tmp % mod;  
        b >>=1;  
    }  
    return  ans;  
}  
LL C(LL n, LL m)  
{  
    if(m>n||m<0)return 0;  
    return  fac[n]*pow(fac[m]*fac[n-m],mod-2)%mod;  
}  
int main(){
    scanf("%s",s+1);
    p=strlen(s+1);
    init();
    for(int i=1;i<=p;i++){
	if(s[i]=='(')
	    lf[i]=lf[i-1]+1;
	else
	    lf[i]=lf[i-1];		
    }
    for(int i=p;i>0;i--){
	if(s[i]==')')
	    rg[i]=rg[i+1]+1;
	else
	    rg[i]=rg[i+1];
    }
    LL ans=0;
    for(int i=1;i<=p;i++){
	if(s[i]=='('){
	    ans+=C(lf[i]+rg[i]-1,rg[i]-1);
	    ans%=mod;
	}	
    }
    printf("%lld\n",ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值