Anton and School - 2 CodeForces - 785D 范德蒙恒等式

题目传送门:Problem - 785D - Codeforces

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 \frac{n}{2} charactes of the sequence are equal to "(". 
  • Last \frac{n}{2} 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 10^{9}+7 .

样例输入:

)(()()
()()()

样例输出:

6

7

题目大意:

        给你一串只包含左右括号的字符串,问你字符串的字串中有多少不同组合成左半边全左括号,右半边全右括号的合法括号组。

解题思路:

        题目很简单,遍历字符串,计算第i个字符左边的‘('个数,以及右边')'的个数,需提前遍历一遍,计算')'的数量,那么每次遍历到'(',我们就可以让ans+=\sum_{i=1}^{l}{C_{l-1}}^{i-1}*{C_{r}}^{i},这里如果遍历的话,显然会超时,这时,我们就有一个等式,又叫范德蒙恒等式:

\sum_{i=0}^{min(l-1,r-1)}{C_{l-1}}^{i}*{C_{r}}^{i+1}={C_{l+r-1}}^{l},

所以我们每次遍历到')'时r--,遍历到'('时l++,同时计算一次答案,需要用到组合数学,可以先打表,把阶乘以及阶乘的逆元都提前算出来,接着就是计算答案了,看代码。

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=2e5+10;
ll fac[N],infac[N];
char str[N];
ll ksm(ll a)
{
    ll res=1,k=mod-2;
    while(k)
    {
        if(k&1) res=(res%mod*a%mod)%mod;
        k>>=1;
        a=(a%mod*a%mod)%mod;
    }
    return res;
}
void init()
{
    fac[0]=1;
    infac[0]=1;
    for (int i=1; i<N; i++) {
        fac[i]=fac[i-1]*i%mod;
        infac[i]=infac[i-1]*ksm(i)%mod;
    }
}
int main()
{
    init();
    while(scanf("%s",str)!=EOF)
    {
        ll l=0,r=0,ans=0;
        for (int i=0; i<strlen(str); i++) {
            if(str[i]==')') r++;
        }
        for (int i=0; i<strlen(str); i++) {
            if(str[i]==')') r--;
            else{
                l++;
                ans=(ans%mod+(fac[l+r-1]%mod*infac[r-1]%mod*infac[l]%mod)%mod)%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值