CodeForces - 785D(组合数学)

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.
题意:
给定一串含有’(‘和’)'的字符串,可以删除该串中任意的字符,使串中的括号合法,求有多少种方案。
没有读懂题,题意是从大佬博客看懂的,然后大概懂了题意,大佬博客都用了“范德蒙恒等式”,我的解法是研究了lt学长的代码之后懂得,感谢大佬们。
从前往后数出左括号的前缀和,从后往前输出右括号的后缀和,然后从前往后判断,如果该位是左括号,将当前位置之后的右括号数量比作盒子,将当前位置之前的左括号比作球,问题便转化成了,将相同的球放到不同盒中,盒子可以为空的方案数,将方案数加起来便是答案
贴一下代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 200500;
const ll mod = 1e9+7;
ll A[N],inv[N];
ll quick_mod(ll a,ll b) {
	ll ans = 1 % mod;
	while(b) {
		if(b & 1) ans = ans * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}
void init() {
	A[0] = 1;
	for(int i = 1; i < N; ++i)
		A[i] = A[i - 1] * i % mod;
	inv[N - 1] = quick_mod(A[N - 1],mod - 2);
	for(int i = N - 2; i >=0; --i)
		inv[i] = inv[i + 1] * (i + 1) % mod;
}
ll C(int a,int b) {
	return A[a] * inv[b] % mod * inv[a - b] % mod;
}
char s[N];
int L[N],R[N];
int main() {
	init();
	scanf("%s",s+1);
	int n=strlen(s+1);
	for(int i = 1; i <= n; ++i) {
		if(s[i] == '(') {
			L[i] = L[i - 1] + 1;
		} else {
			L[i] = L[i - 1];
		}
	}
	for(int i = n; i >= 0; --i) {
		if(s[i] == ')') {
			R[i] = R[i + 1] + 1;
		} else {
			R[i] = R[i + 1];
		}
	}
	ll ans = 0;
	for(int i = 1; i <= n; ++i) {
		if(s[i] == '(') {
			ans = (ans + C(L[i] + R[i] - 1,R[i] - 1)) % mod;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

求排列数是模板,今天重新打了一遍,发现还是有地方有一点忘记了,应该是没有需到位,顺便复习了一下放球到盒子中的八类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值