博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)
阅读量:5073 次
发布时间:2019-06-12

本文共 3818 字,大约阅读时间需要 12 分钟。

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

 

 

题目链接:

这道题实际上就算不能过也可以是可以写一下的,就是基本会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 
#include
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
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;}

转载于:https://www.cnblogs.com/Blackops/p/6694196.html

你可能感兴趣的文章
Jquery ui widget开发
查看>>
关于indexOf的使用
查看>>
英语单词
查看>>
centos6.8下安装matlab2009(图片转帖)
查看>>
Mongo自动备份
查看>>
cer证书签名验证
查看>>
新手Python第一天(接触)
查看>>
【bzoj1029】[JSOI2007]建筑抢修
查看>>
synchronized
查看>>
codevs 1080 线段树练习
查看>>
[No0000195]NoSQL还是SQL?这一篇讲清楚
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
Python-Web框架的本质
查看>>
QML学习笔记之一
查看>>
Window 的引导过程
查看>>
App右上角数字
查看>>
从.NET中委托写法的演变谈开去(上):委托与匿名方法
查看>>
小算法
查看>>
201521123024 《java程序设计》 第12周学习总结
查看>>
新作《ASP.NET MVC 5框架揭秘》正式出版
查看>>