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

Copy

)(()()

output

Copy

6

input

Copy

()()()

output

Copy

7

input

Copy

)))

output

Copy

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 1, 2, 3 and 4, we will get the subsequence "()".
  • If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()".
  • If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 1, 3, 5 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.

题意:。当一个串的'('都在左边,所有的')'那么这个串是好串。。现给一个括号串,问子串中有多少个这样的好串。

做法:自己想了很久没点思路,搜的题解,是“范德蒙恒等式”,第一次见。学习来自:https://blog.csdn.net/zengaming/article/details/63684635

先记录每个字符左边有多少个'(',右边有多少个')',包含当前字符本身;然后从左往右遍历字符串,如果当前字符是'(',左边(包含本身)有a个'(',右边有b个')',那么满足条件的子字符串就增加了

以下图片来自 https://blog.csdn.net/zengaming/article/details/63684635

范德蒙式如下:


AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=2e5+10;
ll f[N];
string s;
int a,b;
ll ans;
ll powmod(ll a,ll b){ll res=1;for(;b;b>>=1){
		if(b&1) res=res*a%mod;a=a*a%mod;}return res;}
ll inv(ll x){
	return powmod(x,mod-2);
}
ll C(ll n,ll m){
	return f[n]*inv(f[m])%mod*inv(f[n-m])%mod;
}
int main(){
	f[0]=1;
	for(ll i=1;i<=2e5+5;i++){
		f[i]=f[i-1]*i%mod;
	}
	cin>>s;
	for(int i=0;i<s.size();++i) if(s[i]==')') ++b;
	for(int i=0;i<s.size();++i) {
		if(s[i]=='('){
			++a;
			ans=(ans+C(a+b-1,a))%mod;
		}
		else --b;
	}
	printf("%lld\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙大学ccsu_deer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值