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

来源:互联网 发布:淘宝联盟无法分享微信 编辑:程序博客网 时间:2024/04/29 09:29

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.


题目大意;

问你一共有多少个子串,满足下列条件:

①长度为偶数

②前一半是(

③后一半是)

④子串不必要连续。


思路:


我们O(n)枚举分界线,那么对应左侧有n个(,右边有m个)

假设n<=m,有公式6的飞起:




那么维护一波前缀和即可。。。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;#define mod 1000000007#define ll __int64const ll N = 1000000+5;const ll M = 1000000+5;ll pre[2000500];ll bac[2000500];char a[2000500];ll fac[1000050];            //阶乘ll inv_of_fac[1000005];        //阶乘的逆元ll qpow(ll x,ll n){    ll ret=1;    for(; n; n>>=1)    {        if(n&1) ret=ret*x%mod;        x=x*x%mod;    }    return ret;}void init(){    fac[1]=1;    for(int i=2; i<=M; i++)        fac[i]=fac[i-1]*i%mod;    inv_of_fac[M]=qpow(fac[M],mod-2);    for(int i=M-1; i>=0; i--)        inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod;}ll C(ll a,ll b){    if(b>a) return 0;    if(b==0) return 1;    return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod;}int main(){    while(~scanf("%s",a+1))    {        init();        int n=strlen(a+1);        memset(pre,0,sizeof(pre));        memset(bac,0,sizeof(bac));        for(int i=1;i<=n;i++)        {            if(a[i]=='(')pre[i]=pre[i-1]+1;            else pre[i]=pre[i-1];        }        for(int i=n;i>=1;i--)        {            if(a[i]==')')bac[i]=bac[i+1]+1;            else bac[i]=bac[i+1];        }        ll ans=0;        for(int i=1;i<=n;i++)        {            if(a[i]==')')continue;            else            {                int nn=pre[i];                int mm=bac[i+1];                ans+=C(nn+mm-1,mm-1);                ans%=mod;            }        }        printf("%I64d\n",(ans+mod)%mod);    }}




0 0
原创粉丝点击