您的位置:首页 > 其它

Codeforces 785D Anton and School - 2 组合数学

2017-06-29 19:39 435 查看
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


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.

对于每一个'(',记它左边有n个‘(’,右边有m个')',则共有 ∑ni=1(Cn−in−1∗Cim) = Cnn+m−1 种方案,这个结论可以由范德蒙恒等式得到。

范德蒙恒等式:


字符串长度最多为200000,直接计算肯定会超时。

这时候,我们可以利用逆元求大组合数。

什么是逆元?求(a/b)mod n时,如果a和b太大,我们可以将式子转化为(a*x)mod n,则x在模数n下与a构成逆元。

这里,因为10^9+7是一个质数,我们可以利用费马小定理的结论:(a/b)mod n且n为素数时,逆元就是b的n-2次方。

将组合数表示成阶乘的形式,然后利用上述结论快速幂求逆元,就可以将时间复杂度降到O(nlogn).

#include <cstdio>
#include <iostream>
#include <stdio.h>
using namespace std;
const long long mod=1e9+7;
long long p[200001];
long long a[200001],b[200002];
int n,i,j;
long long ans=0;
char ch[200001];

long long fastpow(long long base,long long index) {
long long sum=base,an=1;
long long i=index;
while (i) {
if (i%2) an=(an*sum)%mod;
sum*=sum;
sum=sum%mod;
i/=2;
}
return an;
}

long long lucas(long long n,long long m){
if (n<m) {
return 0;
} else return p
*(fastpow((p[m]*p[n-m])%mod,mod-2)%mod)%mod;
}

int main () {
n=0;
a[0]=0;
p[0]=1;
while (scanf("%c",&ch[++n])!=EOF) {
if (ch
=='(') a
=a[n-1]+1; else a
=a[n-1];
p
=(p[n-1]*n)%mod;
}
b[n+1]=0;
for (i=n;i>=1;i--) {
if (ch[i]==')') b[i]=b[i+1]+1; else b[i]=b[i+1];
}
for (i=1;i<=n;i++) {
if (ch[i]=='(') {
ans+=lucas(a[i]+b[i]-1,b[i]-1);
ans%=mod;
}
}
cout << ans << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  组合数学