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 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


题解:

对于每一个当前为"("的位置i,设a为区间[1,i]中左括号的个数,b为区间(i,len]中右括号的个数,

那么在保证当前位置的左括号留下,当前位置右面的左括号全部删除的前提下,合法的删法数


最后的答案ans就是


其中组合数用阶乘打个表好了

可是这样写是n^2复杂度,所以还需要用范德蒙恒等式优化下,

范德蒙德恒等式如下:




①C(n,0)*C(m,0)+C(n,1)*C(m,1)+…+C(n,m)*C(m,m) == C(n+m,m)

②C(n,0)*C(m,k)+C(n,1)*C(m,k-1)+…+C(n,k)*C(m,0) == C(n+m,k)

③C(n,0)*C(n,1)+C(n,1)*C(n,2)+…+C(n,n-1)*C(n,n) == C(2*n,n-1)

由②得,显而易见了:K[x] = C(b+a-1, a);


#include<stdio.h>
#include<string.h>
#define LL long long
#define mod 1000000007
char str[200005];
LL q[200005], jc[300005] = {1};
LL Pow(LL a, LL b)
{
	LL sum;
	sum = 1;
	while(b)
	{
		if(b%2==1)  sum = (sum*a)%mod, b--;
		else  a = (a*a)%mod, b /= 2;
	}
	return sum;
}
LL C(LL n, LL m)      
{  
	LL ans;
	if(n<m)
		return 0;
	ans = (jc[n]*Pow((jc[m]*jc[n-m])%mod, mod-2)%mod)%mod;
	return ans;
}
int main(void)
{
	LL i, n, a, b, ans;
	for(i=1;i<=300000;i++)
		jc[i] = (jc[i-1]*i)%mod;
	scanf("%s", str+1);
	n = strlen(str+1);
	for(i=n;i>=1;i--)
	{
		q[i] = q[i+1];
		if(str[i]==')')
			q[i] += 1;
	}
	a = b = ans = 0;
	for(i=1;i<=n;i++)
	{
		if(str[i]=='(')
		{
			a += 1;
			b = a+q[i+1]-1;
			ans = (ans+C(b, a))%mod;
		}
	}
	printf("%I64d\n", ans);
	return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值