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

 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 sequences of lengthn 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 sequences. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence ofs 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 modulo109 + 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 exceed200 000.

Output

Output one number — the answer for the task modulo 109 + 7.

Examples
Input
)(()()
Output
6
Input
()()()
Output
7
Input
)))
Output
0

题意:条件:当一个偶数长度的字符串为非空,并且前n/2的字符都是' ( ' 和后n/2的字符都是' ) ' 时 ,'定义该字符串是RSBS。
input给一个只包含' ( '和' ) '的字符串,问其子序列构成的字符串是RSBS的个数。(答案取%1e9+7)

题解:
for字符串的每个位置,设当前位置之前的左括号数为Lnum(不包括当前),当前位置之后的右括号数为Rnum,当 当前位置为的字符为左括号时,计算新出现RSBS的个数,当前新出现的RSBS个数为

即:总的新出现的RSBS个数 = 前Lnum+1个左括号与后Rnum个右括号满足RSBS的个数 — 前Lnum个左括号与后Rnum个右括号满足RSBS的个数;
由范德蒙恒等式可得


可化简为


代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long LL;
const int P=1000000007;
LL f[1000001],v[1000001];
LL rp(LL now, int k) 
{
    LL will=1;
    for (; k; k>>= 1, now *= now, now %= P) 
    {
        if (k & 1) will *= now, will %= P;
    }
    return will;
}
LL C(int n, int m) 
{
    if(n < m) return 0;
    return f[n] * rp(f[m], P - 2) % P * rp(f[n - m], P - 2) % P;
}
void init()
{
    f[0] = 1; v[0] = 1;
    for (int i = 1; i <= 1000000; i++) //1e6以内的组合数
    {
        f[i] = f[i - 1] * i % P;
    }
}
char a[200005];
LL sum[200005][2];
int main()
{
	init();
	scanf("%s",a);
	LL l=strlen(a);
	if(a[0]=='(')sum[0][0]++;
	else sum[0][1]++;
	for(int i=1;i<l;i++)
	{
		sum[i][0]=sum[i-1][0];
		sum[i][1]=sum[i-1][1];
		if(a[i]=='(')sum[i][0]++;
		else sum[i][1]++;
	}
	LL ans=0;
	for(int i=0;i<l;i++)
	{
		if(a[i]=='(')
		{
			LL n=sum[l-1][1]-sum[i][1];
			LL m=sum[i][0]-1;
			LL mi1=min(m,n);
			LL mi2=min(m+1,n);
			ans=(ans+C(n+m+1,mi2)-C(n+m,mi1)+P)%P;
		}
	}
	printf("%lld\n",ans%P);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值