Codeforces Round #404 (Div. 2) D. Anton and School - 2(范德蒙恒等式)

185 篇文章 0 订阅
116 篇文章 0 订阅

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

题意略。

分析:我们考虑直接枚举左右括号的分界点,设左边有n个(右边有m个),从左向右枚举的话n每次只能多1,我们考虑新多的这个(对答案的贡献,设f(n,m)为左边连续n个”(“右边连续m个”)”的序列的答案,那么很显然答案等于
严格的说还要减去都是空的那种情况
,然后对这个式子做个变形就是
变换

,第二步到第三步用到了范德蒙恒等式,有了f(n,m) = C(n+m,m)这个结论,很容易得到新加进来的
左括号对答案的贡献为f(n’,m’) - f(n,m’).预处理一下阶乘前缀和其逆元就可以了.

#include <bits/stdc++.h>
#define MOD 1000000007
#define N 200005 
using namespace std;
int n,m,ans,pre[N],suc[N];
long long p[N];
char s[N];
long long inv(long long a,long long m)  
{  
    if(a == 1)return 1;  
    return inv(m % a,m)*(m - m / a) % m;  
}  
long long f(int x,int y)
{
    long long ans = p[x];
    ans = (ans * inv(p[y],MOD)) % MOD;
    ans = (ans * inv(p[x-y],MOD)) % MOD;
    return ans;
}
int main()
{
    scanf("%s",&s);
    n = strlen(s);
    p[0] = 1;
    for(int i = 1;i <= n;i++) p[i] = (p[i-1] * i) % MOD;
    for(int i = 1;i <= n;i++) pre[i] = pre[i-1] + (s[i-1] == '(');
    for(int i = n;i;i--) suc[i] = suc[i+1] + (s[i-1] == ')');
    for(int i = 1;i <= n;i++)
     if(pre[i] != pre[i-1]) ans = (ans + f(pre[i]+suc[i],pre[i]) - f(pre[i-1]+suc[i],suc[i]) + MOD) % MOD;
    cout<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值