D. 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.

这个题问题做不出来,绝对是自己数学的见得东西还会太少了。

这个东西,感觉要是用组合方法 证明的话,还是很简单的.

   甲班有个同学,乙班有个同学,从两个班中选出个一共有种不同的选法。而换一种思维方式

从甲班中选取个同学,从乙班中选取个同学,共有种方法,而对所有的

就是

可以看出这两种方法应该是相等的.

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int mod=1e9+7;
const int N=3e5;
char s[N];
LL f[N],h[N],g[N];
LL powmod(LL a, LL b,LL p)
{
    LL ans = 1;
    a %= p;
    while(b)
    {
        if(b & 1)
        {
            ans = ans * a % p;
            b--;
        }
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}
void inist()
{
    f[0]=1; f[1]=1;
    for(int i=1;i<=200000;i++)
    {
        f[i]=f[i-1]*i%mod;
    }
}
LL C(int n,int m)
{
    if(m>n) return 0;
    if(n==0) return 0;
    LL temp=f[n]*(powmod(f[m]*f[n-m],mod-2,mod));
    return temp%mod;
}
int main()
{
    inist();
    int n;
    cin>>s;
    n=strlen(s);
    for(int i=0;i<n;i++)
    {
        if(i!=0)
        h[i]=h[i-1];
        if(s[i]==‘(‘) h[i]++;
    }
    for(int i=n-1;i>=0;i--)
    {
        g[i]=g[i+1];
        if(s[i]==‘)‘) g[i]++;
    }
    LL ans=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]==‘)‘)
        {
            //cout<<h[i]<<" "<<g[i+1]<<endl;
            ans+=C(h[i]+g[i+1],g[i+1]+1);
            ans%=mod;
           // cout<<ans<<endl;
        }
    }
    cout<<ans<<endl;
}

  

时间: 2024-12-05 04:38:18

D. Anton and School - 2 范德蒙恒等式的相关文章

CodeForces 785 D Anton and School - 2 范德蒙恒等式

Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdou

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

题意:给你一个由'('和')'组成的字符串,问你有多少个子串,前半部分是由'('组成后半部分由')'组成 思路:枚举这个字符串中的所有'('左括号,它左边的所有'('左括号的个数为num1,它的右边的所有')'右括号的个数为num2, 根据范德蒙恒等式计算得出 代码: #include <bits/stdc++.h> #define ll long long #define maxn 200000 #define mod 1000000007 using namespace std; ll j

范德蒙恒等式

[范德蒙恒等式] 甲班有个同学,乙班有个同学,从两个班中选出个一共有种不同的选法.而换一种思维方式从甲班中选取个同学,从乙班中选取个同学,共有种方法,而对所有的就是范德蒙恒等式. 下面的形式也叫范德蒙恒等式.是特殊形式.此形式中,k=n=m.

范德蒙恒等式的证明

今天我们来认识组合数学中一个重要的恒等式---范德蒙恒等式.这个恒等式的表述如下 很自然的公式,接下来一起来看看它的证明,在维基百科上给出了两种方法证明,分别如下 (1)组合方法证明     甲班有个同学,乙班有个同学,从两个班中选出个一共有种不同的选法.而换一种思维方式 从甲班中选取个同学,从乙班中选取个同学,共有种方法,而对所有的 就是 可以看出这两种方法应该是相等的,即 (2)生成函数法证明 由于,对于等式左边有 而对于等式右边有 左右两边一比较可知 成立,证明完毕! 接下来我们看看一些关

6476. 【GDOI2020模拟02.19】A(范德蒙恒等式)

题目描述 题解 镇♂男则反 容斥下界,上界开到大概505位,数位dp最终的和V 设边界(要大于边界)之和为S,那么答案为C(V-S-1,n-1) 根据范德蒙恒等式,C(n+m,k)=∑C(n,i)*C(m,k-i) 如果nm都是正数很好证明,把n+m分成n和m两部分,枚举n部分选择个数组合一下 这个式子其实可以拓展到负数,证明要用生成函数 关于n为负数的组合数:\(C(n,m)=n^{\underline{m}}/m!\),其实和正数时是一样的 (注意这只是为了计算范德蒙恒等式而扩展的,在一般情

浅谈范德蒙德(Vandermonde)方阵的逆矩阵的求法以及快速傅里叶变换(FFT)中IDFT的原理

浅谈范德蒙德(Vandermonde)方阵的逆矩阵与拉格朗日(Lagrange)插值的关系以及快速傅里叶变换(FFT)中IDFT的原理 只要稍微看过一点线性代数的应该都知道范德蒙德行列式. \[V(x_0,x_1,\cdots ,x_{n-1})=\begin{bmatrix} {1}&{1}&{\cdots}&{1}\{x_{0}}&{x_{1}}&{\cdots}&{x_{n-1}}\{x_{0}^2}&{x_{1}^2}&{\cdots

范德蒙行列式

首先,在讲范德蒙行列式之前,先看下一个具体的例子,如下图是一个四阶行列式, 我们能观察出来,从第四行开始,后行减去前行的2倍: 需要特别注意的是,各个运算次序不能颠倒,这是因为后一次运算是作用在前一次运算的结果上的缘故. 后面的步骤与方法,与前面的相似,后行减掉前行的n倍(n根据具体的数字来确定),重复这样的步骤方法算降阶到2阶行列式,就可得出答案,答案为,(4-2)*(7-2)*(9-2)*(7-4)*(9-4)*(9-7)=2100: 根据以上的例子,再采用数学归纳法,可以证明得出形如像如下

CodeForces 785D Anton and School - 2

枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的方案数. 即在它左边取$k$个左括号,在右边取$k-1$个右括号都是合法的方案,这个东西可以用范德蒙恒等式化成一个组合数以及容斥原理计算. 范德蒙恒等式:http://blog.csdn.net/acdreamers/article/details/31032763 #include <cstdio

Codeforces Round #404 (Div. 2) C 二分,水 D 数学,好题 E 分块

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 题意:仓库容量n,每天运来m粮食,第 i 天被吃 i 粮食,问第几天仓库第一次空掉. tags:==SB题 注:二分边界判断,数据范围爆long long判断. // CF404 C #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000&