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!
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 one number — the answer for the task modulo 109 + 7.
)(()()
6
()()()
7
)))
0
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.
O(n^2):
定义: f[i]为以第i个括号(当第i个括号为左括号时计算)为最后一个左括号的方案数
nowx表示1~i中左括号的个数,nowy表示i+1~n中右括号的个数
f[i]=ΣC(nowx-1,j-1)*C(nowy,j)=ΣC(nowx-1,j-1)*C(nowy,nowy-j)
构造法优化: 看做从nowx+nowy-1个物体中选出nowy-1个的方案,即为从nowx里选出j-1个和在nowy中选出nowy-j个的方案数,即为上式.
故上式可简化为 f[i]=C(nowx+nowy-1,nowy-1)
预处理出 阶乘和阶乘的逆元,O(n)即可
代码:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
//#include<bits/stdc++.h>
#define mod 1000000007
#define N 500050
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n;
ll qm(ll a,ll b)
{
ll ret=1;
while(b)
{
if(b&1)ret=ret*a%mod;
a=a*a%mod;b>>=1;
}return ret;
}
ll pre[N],inv[N];
void init()
{
pre[0]=1;
for(int i=1;i<=200000;i++)
pre[i]=pre[i-1]*i%mod;
for(int i=0;i<=200000;i++)
inv[i]=qm(pre[i],mod-2);
}
ll C(int n,int m)
{
return pre[n]*inv[m]%mod*inv[n-m]%mod;
}
char ch[N];
ll ans;
int main()
{
init();
scanf("%s",ch+1);
n=strlen(ch+1);
int nowx=0,nowy=0;
for(int i=n;i;i--)
if(ch[i]==')')nowy++;
for(int i=1;i<=n;i++)
{
if(ch[i]==')')nowy--;
else
{
nowx++;
ans+=C(nowx+nowy-1,nowy-1);
ans%=mod;
}
}printf("%lld\n",ans);
}