Codeforces 785D Anton and School

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

题目链接:CF 785D

这道题实际上就算不能过也可以是可以写一下的,就是基本会TLE……记当前位置为x,[1,x]中的左括号个数为L,[x+1,len]中右括号个数为R,可以发现每次增加一个 '(',可以跟右边组合的情况多了$$sum_{i=0}^{min(L-1,R-1)}inom{L-1}{i} * inom{R}{i+1}$$

这个式子把右边的组合数又可以化成$$sum_{i=0}^{min(L-1,R-1)}inom{L-1}{i} * inom{R}{R-i-1}$$,可以发现下面之和是一个常数即$R-1$,这个时候就出现了很厉害的公式——范德蒙恒等式

然后就不用每一次都for一遍把组合数加起来,而是加上组合数$$inom{L-1+R}{R-1} $$就行。

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 200010;
const LL MOD = 1e9 + 7;
char s[N];
LL fac[N], inv[N];
int preL[N], preR[N];

LL qpow(LL a, LL b, LL m)
{
    LL r = 1LL;
    while (b)
    {
        if (b & 1)
            r = r * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return r;
}
void init()
{
    fac[0] = 1LL;
    inv[0] = 1LL;
    for (LL i = 1; i < N; ++i)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = qpow(fac[i], MOD - 2, MOD);
    }
}
LL combine(LL n, LL m, LL mod)
{
    LL ret = ((fac[n] * inv[m]) % mod * inv[n - m]) % mod;
    return ret;
}
int main(void)
{
    init();
    int i;
    while (~scanf("%s", s + 1))
    {
        CLR(preL, 0);
        CLR(preR, 0);
        int len = strlen(s + 1);
        for (i = 1; i <= len; ++i)
        {
            preL[i] = preL[i - 1] + (s[i] == '(');
            preR[i] = preR[i - 1] + (s[i] == ')');
        }
        LL ans = 0LL;
        for (i = 1; i <= len; ++i)
        {
            if (s[i] == '(')
            {
                int rightR = preR[len] - preR[i];
                ans = ans + combine(preL[i] - 1 + rightR, rightR - 1, MOD);
                if (ans > MOD)
                    ans %= MOD;
            }
        }
        printf("%I64d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Blackops/p/6694196.html