#404 (div2)Anton and School - 2

Posted Wally的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#404 (div2)Anton and School - 2相关的知识,希望对你有一定的参考价值。

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来表示吧。然后我们枚举每个位置,如果当前位置是‘(‘那么答案应该增加sigmaC(n-1,i)+C(m,i+1),可能有人会看不懂,我先解释一下,因为每扫到一个位置是‘(‘,那么增加的部分一定拥有这个位置的‘(‘,所以左边n要-1,右边i要+1。然后这里如果每一个位置都这样求的话,复杂度是O(n2),很明显不行。所以剩下的就是化简组合数了。。讲道理我组合数这么渣还玩这个。。。我们来看下,右边的C(m,i+1)等价于C(m,m-i-1),那么原式=sigmaC(n-1,i)+C(m,m-i-1),我们可以发现取数的和是个定值,为m-1,固等价于sigmaC(n+m-1,m-1),然后再套逆元,这道题就解决了。。。

知识点:1.求a关于mod(m+2)的逆元

 1 long long pow(long long a,long long m)
 2 {
 3     long long tmp=a%mod;
 4     long long ans=1;
 5     while(m)
 6     {
 7         if(m&1)
 8         ans=(ans*tmp)%mod;
 9         tmp=tmp*tmp%mod;
10         m=m>>1;
11     }
12     return ans;
13 }

2.(a/b)%mod的求法

 1 long long pow(long long a,long long m)
 2 {
 3     long long tmp=a%mod;
 4     long long ans=1;
 5     while(m)
 6     {
 7         if(m&1)
 8         ans=(ans*tmp)%mod;
 9         tmp=tmp*tmp%mod;
10         m=m>>1;
11     }
12     return ans;
13 }
14 long long  solve(long long n,long long m)
15 {
16     if(m>n||m<0) return 0;
17     return cnt[n]*pow(cnt[m]*cnt[n-m],mod-2)%mod;
18 }

3.C(n,m)%mod的求法(n!爆long long的情况下)

 1 void init()
 2 {
 3     cnt[0]=1;
 4     for(int i=1; i<maxn; i++)
 5         cnt[i]=(cnt[i-1]*i)%mod;
 6     return ;
 7 }
 8 long long pow(long long a,long long m)
 9 {
10     long long tmp=a%mod;
11     long long ans=1;
12     while(m)
13     {
14         if(m&1)
15         ans=(ans*tmp)%mod;
16         tmp=tmp*tmp%mod;
17         m=m>>1;
18     }
19     return ans;
20 }

AC代码:

技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 #include <math.h>
 4 #include <cstdio>
 5 using namespace std;
 6 const int maxn=200005;
 7 const int mod=1e9+7;
 8 char s[maxn];
 9 long long a[maxn],b[maxn],cnt[maxn];
10 void init()
11 {
12     cnt[0]=1;
13     for(int i=1; i<maxn; i++)
14         cnt[i]=(cnt[i-1]*i)%mod;
15     return ;
16 }
17 long long pow(long long a,long long m)
18 {
19     long long tmp=a%mod;
20     long long ans=1;
21     while(m)
22     {
23         if(m&1)
24         ans=(ans*tmp)%mod;
25         tmp=tmp*tmp%mod;
26         m=m>>1;
27     }
28     return ans;
29 }
30 long long  solve(long long n,long long m)
31 {
32     if(m>n||m<0) return 0;
33     return cnt[n]*pow(cnt[m]*cnt[n-m],mod-2)%mod;
34 }
35 int main()
36 {
37     //freopen("data.txt","r",stdin);
38     //freopen("out.txt","w",stdout);
39     init();
40     while(~scanf("%s",s+1))
41     {
42         int len=strlen(s+1);
43         memset(a,0,sizeof(a));
44         memset(b,0,sizeof(b));
45         for(int i=1; i<=len; i++)
46             if(s[i]==()
47                 a[i]=a[i-1]+1;
48             else
49                 a[i]=a[i-1];
50         for(int i=len; i>0; i--)
51         {
52             if(s[i]==))
53                 b[i]=b[i+1]+1;
54             else
55                 b[i]=b[i+1];
56         }
57         long long ans=0;
58         for(int i=1; i<=len; i++)
59         {
60             if(s[i]==()
61             {
62                 ans=(ans+solve(a[i]+b[i]-1,b[i]-1))%mod;
63             }
64         }
65         printf("%I64d\n",ans);
66     }
67     return 0;
68 }
View Code

 

以上是关于#404 (div2)Anton and School - 2的主要内容,如果未能解决你的问题,请参考以下文章

codeforces round #324(div2) E题 Anton and lra

CF #404 (Div. 2) D. Anton and School - 2 (数论+范德蒙恒等式)

Anton and Fairy Tale (公式)

Anton and Fairy Tale (公式)

[二分] Codefoces Anton and Making Potions

Anton and School - 2 (范德蒙恒等式模板)