您的位置:首页 > 其它

Codeforces Round #404 (Div. 2) D. Anton and School - 2 前缀的后缀、 范德蒙恒等式、容斥

2017-03-16 01:44 721 查看
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.

Source

Codeforces Round #404 (Div. 2)

My Solution

题意:给出一个只有"("和“)"的字符串,为有多少个子序列,它的长度为len,则左边len/2个字符为”("右边len/2个字符为")",问这样的子序列有多少个。

前缀的后缀、 范德蒙恒等式、容斥

子串为 前缀的后缀,这里是子序列,所以s[i]为必取,作为序列的最后一个元素,然后前面的"("为选取,

所以可以预处理出suml和sumr,分表表示i的左边有多少个"(", i的右边有多少个")".

然后对于每个“(",必选这一个"(",然后i之前的(进行排列组合,选j个"(",就在sumr[i+1]里选j个")"。

所以就是sigma{C(a, j) * C(b, j)},然后这里恰好有个叫做范德蒙恒等式的公式,

 


范德蒙恒等式的证明

[组合数]求组合数的几种方法总结

然后根据上面所描述的方法,每次i是必选的,然后进行排列组合。

ans += C(suml[i] + sumr[i+1, suml[i])  - C(suml[i-1] + sumr[i+1], suml[i-1]);

复杂度 O(nlogn)

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 8;
const LL MOD = 1e9 + 7;
LL suml[MAXN], sumr[MAXN], fac[MAXN];
string s;

inline LL mod(LL a)
{
return a - a / MOD * MOD;
}

inline LL pow_mod(LL a, LL i)
{
if(i == 0) return mod(1);
LL t = pow_mod(a, i>>1);
t = mod(t * t);
if(i & 1) t = mod(t * a);
return t;
}

inline LL get(LL a,LL b)
{
return mod(fac[a+b] * pow_mod(mod(fac[a] * fac[b]), MOD - 2));
}

int main()
{
#ifdef LOCAL
freopen("d.txt", "r", stdin);
//freopen("d.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

cin >> s;
int sz = s.size(), i;
fac[0] = 1;
for(i = 1; i <= sz; i++) fac[i] = mod(fac[i-1] * i);
for(i = 0; i < sz; i++){
if(i == 0){
suml[i] = 0;
if(s[i] == '(') suml[i] += 1;
}
else{
suml[i] = suml[i-1];
if(s[i] == '(') suml[i] += 1;
}
}
sumr[sz] = 0;
for(i = sz - 1; i >= 0; i--){
sumr[i] = sumr[i+1];
if(s[i] == ')') sumr[i] += 1;
}
LL ans = 0;
for(i = 0; i < sz; i++){
if(s[i] == '('){
ans = mod(ans + get(suml[i], sumr[i+1]) - get(suml[i-1], sumr[i+1]) );
}
}
while(ans < 0) ans += MOD;
cout << ans << endl;

#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}


Thank you!
                                                                                                           
                                 ------from ProLights
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐