Codeforces 785D 数学

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
Note

In the first sample the following subsequences are possible:

  • If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())".
  • If we delete characters at the positions 123 and 4, we will get the subsequence "()".
  • If we delete characters at the positions 124 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 125 and 6, we will get the subsequence "()".
  • If we delete characters at the positions 134 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 135 and 6, we will get the subsequence "()".

The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.


题意:求有多少种删除方法  使得剩余的串前n/2个字符全部是 (  后n/2个字符全部是 )


题解:

我们定义第i位如果是 ( 则第 i 位必选  后面的 (  全不要  前面的做全排列  这样就保证没有重复的

如果 i 前面有t1个(  i 后面有t2个  )  那么方案数就是  

C(t1,0)*C(t2,1)+C(t1,1)*C(t2,2)+...+C(t1,min(t1,t2-1))*C(t2,min(t1,t2-1)+1)

我们有这个公式

假设n<m

C(n,0)*C(m,0)+C(n,1)*C(m,1)+...+C(n,n)*C(m,n)=C(n+m,m)

变换下

C(n,0)*C(m,m)+C(n,1)*C(m,m-1)+...+C(n,n)*C(m,m-n)=C(n+m,m)

这个数学意义就是  从一个袋子中有n个球和一个袋子有m个球的两个袋子中取出m个球的方案

那么第一个式子就是  从两个袋子中取出m-1个球的方案

所以

C(t1,0)*C(t2,1)+C(t1,1)*C(t2,2)+...+C(t1,min(t1,t2-1))*C(t2,min(t1,t2-1)+1)=C(t1+t2,t2-1)


ps:我的代码中t1是包含当前位的  所以求组合数时候要减去1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
char s[200005];
ll a[200005],b[200005],fac[400005];
ll ans=0;
ll quick(ll a,ll k){
	ll ans=1;
	while(k){
		if(k&1)ans=ans*a%mod;
		a=a*a%mod;
		k/=2;
	}
	return ans;
}
ll C(ll n,ll m){
	return fac[n]*quick(fac[m],mod-2)%mod*quick(fac[n-m],mod-2)%mod;
}
int main(){
	scanf("%s",s+1);
	ll len=strlen(s+1);
	ll i,j;
	fac[0]=1;
	for(i=1;i<=400000;i++)fac[i]=fac[i-1]*i%mod;
	for(i=1;i<=len;i++){
		if(s[i]=='(')a[i]=a[i-1]+1;
		else a[i]=a[i-1];
	}
	for(i=len;i>=1;i--){
		if(s[i]==')')b[i]=b[i+1]+1;
		else b[i]=b[i+1];
	}
	for(i=1;i<=len;i++){
		if(s[i]!='(')continue;
		ll t1=a[i],t2=b[i+1];
		if(t1==0)continue;
		else if(t2==0)break;
		if(t1==1){
			ans+=C(t2,1); 
		}
		else if(t2==1){
			ans+=1;
		}
		else{
			ans+=C(t1-1+t2,t2-1);
			ans=ans%mod;
		}
	}
	cout<<ans<<endl; 
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值