Description

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.

Key

题意:给一大串前后括号组成的字符串,长度为n,从中任取若干括号、按原顺序排列,要求取出的括号前一半都是“(”,后一半都是”)“,问有多少种组合(即组成如((((())))))。如)(()() (没个括号分别编号1~6),有2 42 63 43 65 62 3 4 6 6种。

事后看了题解才AC。很容易想到的是,读取时只存储每一个后括号之前一共有多少个前括号,例如)(()(),有三个后括号,只需存储0 2 3即可(对该数组命名为sum,并假设一共m个后括号)。然后遍历这个sum数组:对于第i个后括号,左侧有sum[i]个前括号,右侧有m-i个后括号(方便起见令ai=sum[i]bi=m-i),以当前后括号为第一个符合符合“((((()))))”的后括号,则当前后括号左侧的后括号都不可能满足,当前后括号右侧的前括号也不满足。则“以当前后括号为第一个符合的后括号”一共有

C1ai∗C0bi+C2ai∗C1bi+C3ai∗C2bi+...+Cmin(ai,bi+1)ai∗Cmin(ai,bi+1)−1bi=∑i=1min(ai,bi+1)Ciai∗Ci−1bi=∑i=1min(ai,bi+1)Cai−iai∗Ci−1bi=∑i=0min(ai−1,bi)Cai−i−1ai∗Cibi

注意a指的是当前后括号左侧的前括号个数,b为当前的后括号右侧后括号个数,故b不包括当前后括号。

最后根据范德蒙恒等式推倒:

若min(ai−1,bi)=ai−1:

∑i=0min(ai−1,bi)Cai−i−1ai∗Cibi=∑i=0ai−1Cai−i−1ai∗Cibi=Cai−1ai+bi

若min(ai,bi+1)=bi+1:

∑i=1min(ai,bi+1)Ciai∗Ci−1bi=∑i=1bi+1Ciai∗Cbi+1−ibi=Cbi+1ai+bi

合起来还是Cmin(ai,bi)ai+bi

那么最终要做的就是把m个组合数求和。因而求组合数也是个难点。这里直接盗用了上面链接的大神的模板。自己写了个求组合数的博客。这里还是预先打表了,这样快很多。

Code

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#define MX 200005
using namespace std;
typedef long long LL; const int p = 1e9 + 7;
int sum[MX] = { 0 }; // store the sum of all the '(' before the last ')'
LL ans = 0; LL fac[200005], fac_exp[200005];
LL ModExp(LL a, LL b, LL p)
{
LL ans = 1;
while (b)
{
if (b & 1)
ans = ans*a%p;
a = a*a%p;
b >>= 1;
}
return ans;
}
LL C(int n, int m)
{
return fac[n] % p * fac_exp[n - m] % p * fac_exp[m] % p;
} int main()
{
fac[0] = fac_exp[0] = 1;
for (int i = 1;i <= 200000;i++)
{
fac[i] = (fac[i - 1] * i) % p;
fac_exp[i] = ModExp(fac[i], p - 2, p);
} //freopen("in.txt", "r", stdin);
string s;
getline(cin, s);
int num_left = 0, num_right = 0;
for (char c : s) {
if (c == '(') ++num_left;
else {
sum[num_right + 1] = sum[num_right] + num_left;
++num_right;
num_left = 0;
}
}
for (int i = 1;i <= num_right;++i) {
int right = num_right - i + 1;
int left = sum[i];
ans += C(right - 1 + left, right);
}
cout << (int)(ans%p);
return 0;
}

[刷题]Codeforces 785D - Anton and School - 2的更多相关文章

  1. [刷题]Codeforces 794C - Naming Company

    http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...

  2. Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)

    D. Anton and School - 2 time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. [刷题codeforces]650A.637A

    650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...

  4. [刷题codeforces]651B/651A

    651B Beautiful Paintings 651A Joysticks 点击可查看原题 651B是一个排序题,只不过多了一步去重然后记录个数.每次筛一层,直到全为0.从这个题里学到一个正确姿势 ...

  5. [刷题]Codeforces 786A - Berzerk

    http://codeforces.com/problemset/problem/786/A Description Rick and Morty are playing their own vers ...

  6. [刷题]Codeforces 746G - New Roads

    Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...

  7. Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]

    题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...

  8. CF刷题-Codeforces Round #481-G. Petya's Exams

    题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...

  9. CF刷题-Codeforces Round #481-F. Mentors

    题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...

随机推荐

  1. 从编译安装Keepalived 到 配置 负载均衡(LVS-DR)

    最近在研究服务器高可用集群 (HA)…… Keepalived 是用C写的软路由.提供负载均衡与高可用特性. 负载均衡利用IPVS技术 高可用通过VRRP协议实现 更难能的贵的是,一直到最近还在更新 ...

  2. Eclipese Mars安装SVN的全步骤

    在做毕业设计的过程中,由于是团队项目,需要用到SVN,而全新的Eclipse Mars从官网下载下来没有SVN插件,需要自己下载. 1.选择Help-->Eclipese MarketPlace ...

  3. spring(二) AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  4. python之函数学习

    #!/usr/bin/env python # # =============================================== # 位置参数说明 # 位置参数 通过参数传递的位置来 ...

  5. POPTEST老李谈Debug和Release的区别(c#)

    POPTEST老李谈Debug和Release的区别(c#)   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  6. BootstrapBootbox居中

    /* 模态框居中样式 */ .bootbox-container { position: fixed; ; ; ; ; ; overflow-y: auto; } .bootbox-container ...

  7. 2 Java对象的创建过程

    JAVA中创建对象直接new创建一个对象,对么对象的创建过程是怎样的呢? 程序运行过程中有许多的对象被创建出来.那么对象是如何创建的呢? 一 对象创建的步骤 1 遇到new指令时,检查这个指令的参数是 ...

  8. web 项目中a标签传值(中文)到后台的乱码问题

    web 项目中a标签传值(中文)到后台的乱码问题 jsp页面中的a标签: .............. <c:forEach items="${sellerList }" v ...

  9. Spark名词解释及关系

    随着对spark的业务更深入,对spark的了解也越多,然而目前还处于知道的越多,不知道的更多阶段,当然这也是成长最快的阶段.这篇文章用作总结最近收集及理解的spark相关概念及其关系. 名词 dri ...

  10. jdk1.8新特性,还不知道的朋友还不看看,1.9都快出来了

    一.接口的默认方法 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法,示例如下:代码如下:interface Formula {     ...