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

题目链接:CF 785D

这道题实际上就算不能过也可以是可以写一下的,就是基本会TLE……记当前位置为x,[1,x]中的左括号个数为L,[x+1,len]中右括号个数为R,可以发现每次增加一个 '(',可以跟右边组合的情况多了$$\sum_{i=0}^{min(L-1,R-1)}\binom{L-1}{i} * \binom{R}{i+1}$$

这个式子把右边的组合数又可以化成$$\sum_{i=0}^{min(L-1,R-1)}\binom{L-1}{i} * \binom{R}{R-i-1}$$,可以发现下面之和是一个常数即$R-1$,这个时候就出现了很厉害的公式——范德蒙恒等式

然后就不用每一次都for一遍把组合数加起来,而是加上组合数$$\binom{L-1+R}{R-1} $$就行。

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 200010;
const LL MOD = 1e9 + 7;
char s[N];
LL fac[N], inv[N];
int preL[N], preR[N]; LL qpow(LL a, LL b, LL m)
{
LL r = 1LL;
while (b)
{
if (b & 1)
r = r * a % m;
a = a * a % m;
b >>= 1;
}
return r;
}
void init()
{
fac[0] = 1LL;
inv[0] = 1LL;
for (LL i = 1; i < N; ++i)
{
fac[i] = fac[i - 1] * i % MOD;
inv[i] = qpow(fac[i], MOD - 2, MOD);
}
}
LL combine(LL n, LL m, LL mod)
{
LL ret = ((fac[n] * inv[m]) % mod * inv[n - m]) % mod;
return ret;
}
int main(void)
{
init();
int i;
while (~scanf("%s", s + 1))
{
CLR(preL, 0);
CLR(preR, 0);
int len = strlen(s + 1);
for (i = 1; i <= len; ++i)
{
preL[i] = preL[i - 1] + (s[i] == '(');
preR[i] = preR[i - 1] + (s[i] == ')');
}
LL ans = 0LL;
for (i = 1; i <= len; ++i)
{
if (s[i] == '(')
{
int rightR = preR[len] - preR[i];
ans = ans + combine(preL[i] - 1 + rightR, rightR - 1, MOD);
if (ans > MOD)
ans %= MOD;
}
}
printf("%I64d\n", ans);
}
return 0;
}

最新文章

  1. 【前端优化之拆分CSS】前端三剑客的分分合合
  2. c#使用多线程的几种方式示例详解
  3. link和@import导入css文件的区别
  4. iOS 重大新漏洞:可绕开苹果审核机制
  5. Linux搭建python环境中cx_Oracle模块安装遇到的问题与解决方法
  6. OPEN资讯
  7. [一]Head First设计模式之【策略模式】(鸭子设计的优化历程)
  8. 浅谈 IE下innerHTML导致的问题
  9. ORACLE总结系列1--network文件夹里的admin的三个文件信息
  10. 编译内核启用iptables及netfilter
  11. Python之文件与目录
  12. fiddler基本功能介绍
  13. 翻译:赋值操作符(:=)(已提交到MariaDB官方手册)
  14. Bayboy功能详解
  15. JAVA自学笔记20
  16. [SoapUI] 从测试套件,测试用例,测试步骤,测试数据各个级别控制是否执行
  17. 2-Twentieth Scrum Meeting-20151220
  18. 【Python全栈-后端开发】嵩天老师-Django
  19. vue 关于npm run build 的小问题
  20. 使用 Edit + MASM 5.0 编译器 + Linker 连接器

热门文章

  1. 2018.6.5 Oracle plsql编程 游标的使用
  2. Java如何将十六进制数转换为十进制数的自编程序
  3. 问题010:在Java中,什么是常量,什么是变量?
  4. swiper动画效果
  5. 1047: [HAOI2007]理想的正方形
  6. .NET下寄宿于控制台的HTTPS监听
  7. CentOS下安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64
  8. python面试题之介绍一下Python中webbrowser的用法
  9. 关于json数据中的多反斜杆转译--StringEscapeUtils.unescapeJava(踩过的坑)
  10. python3中文件的读与写