zoukankan      html  css  js  c++  java
  • D. Anton and School

    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.

    这个题问题做不出来,绝对是自己数学的见得东西还会太少了。

    这个东西,感觉要是用组合方法 证明的话,还是很简单的.

       甲班有个同学,乙班有个同学,从两个班中选出个一共有种不同的选法。而换一种思维方式

        从甲班中选取个同学,从乙班中选取个同学,共有种方法,而对所有的

     就是

                 

        可以看出这两种方法应该是相等的.

    #include<bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int mod=1e9+7;
    const int N=3e5;
    char s[N];
    LL f[N],h[N],g[N];
    LL powmod(LL a, LL b,LL p)
    {
        LL ans = 1;
        a %= p;
        while(b)
        {
            if(b & 1)
            {
                ans = ans * a % p;
                b--;
            }
            b >>= 1;
            a = a * a % p;
        }
        return ans;
    }
    void inist()
    {
        f[0]=1; f[1]=1;
        for(int i=1;i<=200000;i++)
        {
            f[i]=f[i-1]*i%mod;
        }
    }
    LL C(int n,int m)
    {
        if(m>n) return 0;
        if(n==0) return 0;
        LL temp=f[n]*(powmod(f[m]*f[n-m],mod-2,mod));
        return temp%mod;
    }
    int main()
    {
        inist();
        int n;
        cin>>s;
        n=strlen(s);
        for(int i=0;i<n;i++)
        {
            if(i!=0)
            h[i]=h[i-1];
            if(s[i]=='(') h[i]++;
        }
        for(int i=n-1;i>=0;i--)
        {
            g[i]=g[i+1];
            if(s[i]==')') g[i]++;
        }
        LL ans=0;
        for(int i=1;i<n;i++)
        {
            if(s[i]==')')
            {
                //cout<<h[i]<<" "<<g[i+1]<<endl;
                ans+=C(h[i]+g[i+1],g[i+1]+1);
                ans%=mod;
               // cout<<ans<<endl;
            }
        }
        cout<<ans<<endl;
    }
    

      

  • 相关阅读:
    sql server 2008 评估期已过期解决办法 + vs2008破解(转) 狼人:
    发现godaddy亚太机房主机比较慢,怎样转到godaddy美国机房 狼人:
    模拟提交有文件上传的表单(通过http模拟上传文件) 狼人:
    WordPress 批量关闭和打开评论功能 狼人:
    英文seo外链资源整合,怎么样找国外博客资源? 狼人:
    WindowsServer2003+IIS6+ASP+NET+PHP+MSSQL+MYSQL配置说明 |备份于waw.cnblogs.com 狼人:
    Excel表格的35招必学秘技(学会计的快来转载,留着以后用) 狼人:
    MSN去窗口广告方法http://apatch.org/downloads/ 狼人:
    js 实现谷歌浏览器中隐藏标签后setInterval事件暂停 狼人:
    SQL Server 2008过期导致MSSQLSERVER服务无法启动 狼人:
  • 原文地址:https://www.cnblogs.com/Heilce/p/6580918.html
Copyright © 2011-2022 走看看