codeforces round 404 div2 D Anton and School - 2 组合数学
2017-03-26 15:40
495 查看
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
Input
Output
Input
Output
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.
题目描述:给出一个括号的序列,问有多少种方案,使得删去一些字符之后,使得括号序列形成诸如"(((())))"、“(())”这样的先左括号后右括号的序列的形式。
题目思路:枚举每一个左括号出现的位置,假设这个左括号之前有x个左括号(不包括这个左括号),后边有y个右括号,那么以这个左括号为中心括号的合法括号序列一共有
ΣC(x , i) * C(y , i + 1) (0 <= i <= x)个,但这样显然复杂度O(n)没法处理,但是根据组合数知识,有ΣC(x , i) * C(y , i + 1) (0 <= i <= x) = C(x + y , y - 1) (证明
方法是比较(1 + t)^x * (1 + t) ^ y 与(1 + t) ^ (x + y)等号左右两边t^(y - 1)的系数),这样预先处理出组合数,然后O(n)枚举每一个位置,把他们的和相加就可以了。
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxs = 4e5 + 5;
const int maxn = 2e5 + 5;
LL fac[maxs], inv[maxs];
LL q_pow(LL x, LL n)
{
if (n == 0) return 1LL;
LL res = q_pow(x * x % mod, n / 2);
if (n & 1) res = res * x % mod;
return res;
}
void jiecheng()
{
fac[0] = 1LL;
for (LL i = 1; i < maxs; i++){
fac[i] = fac[i - 1] * i % mod;
}
}
void _inv()
{
inv[0] = 1;
for (int i = 1; i < maxs; i++){
inv[i] = q_pow(fac[i], mod - 2);
}
}
LL C(int n, int m)
{
if (n < m) return 0;
return fac
* inv[m] % mod * inv[n - m] % mod;
}
char s[maxn];
int main()
{
jiecheng();
_inv();
scanf("%s" , s);
int len = strlen(s) , rsum = 0;
for(int i = 0 ; i < len ; i++){
if(s[i] == ')')
++rsum;
}
LL ans = 0 , x , y , left = 0 , right = 0;
for(int i = 0 ; i < len ; i++){
if(s[i] == '('){
x = left;
y = rsum - right;
ans = (ans + C(x + y , y - 1LL)) % mod;
}
if(s[i] == '(') ++left;
else ++right;
}
printf("%lld\n" , ans);
return 0;
}
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
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.
题目描述:给出一个括号的序列,问有多少种方案,使得删去一些字符之后,使得括号序列形成诸如"(((())))"、“(())”这样的先左括号后右括号的序列的形式。
题目思路:枚举每一个左括号出现的位置,假设这个左括号之前有x个左括号(不包括这个左括号),后边有y个右括号,那么以这个左括号为中心括号的合法括号序列一共有
ΣC(x , i) * C(y , i + 1) (0 <= i <= x)个,但这样显然复杂度O(n)没法处理,但是根据组合数知识,有ΣC(x , i) * C(y , i + 1) (0 <= i <= x) = C(x + y , y - 1) (证明
方法是比较(1 + t)^x * (1 + t) ^ y 与(1 + t) ^ (x + y)等号左右两边t^(y - 1)的系数),这样预先处理出组合数,然后O(n)枚举每一个位置,把他们的和相加就可以了。
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxs = 4e5 + 5;
const int maxn = 2e5 + 5;
LL fac[maxs], inv[maxs];
LL q_pow(LL x, LL n)
{
if (n == 0) return 1LL;
LL res = q_pow(x * x % mod, n / 2);
if (n & 1) res = res * x % mod;
return res;
}
void jiecheng()
{
fac[0] = 1LL;
for (LL i = 1; i < maxs; i++){
fac[i] = fac[i - 1] * i % mod;
}
}
void _inv()
{
inv[0] = 1;
for (int i = 1; i < maxs; i++){
inv[i] = q_pow(fac[i], mod - 2);
}
}
LL C(int n, int m)
{
if (n < m) return 0;
return fac
* inv[m] % mod * inv[n - m] % mod;
}
char s[maxn];
int main()
{
jiecheng();
_inv();
scanf("%s" , s);
int len = strlen(s) , rsum = 0;
for(int i = 0 ; i < len ; i++){
if(s[i] == ')')
++rsum;
}
LL ans = 0 , x , y , left = 0 , right = 0;
for(int i = 0 ; i < len ; i++){
if(s[i] == '('){
x = left;
y = rsum - right;
ans = (ans + C(x + y , y - 1LL)) % mod;
}
if(s[i] == '(') ++left;
else ++right;
}
printf("%lld\n" , ans);
return 0;
}
相关文章推荐
- Codeforces Round #404 (Div. 2) D. Anton and School - 2(组合数学)
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 排列组合好题
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 前缀的后缀、 范德蒙恒等式、容斥
- Codeforces Round #404 (Div. 2) D. Anton and School - 2
- Codeforces Round #379 (Div. 2) F. Anton and School
- [排列组合]Codeforces Round #324 (Div. 2)E - Anton and Ira
- Codeforces Round #379 (Div. 2) F. Anton and School
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
- Codeforces Round #330 (Div. 2)——B 数学——Pasha and Phone
- 【Codeforces Round 354 (Div 2)E】【数学 多项式除法 讨论】The Last Fight Between Human and AI 多项式除以x-k是否值整除
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya(组合数学)
- Codeforces Round #324 (Div. 2) 584B. Kolya and Tanya 组合数学
- Codeforces Round #199 (Div. 2) -- C. Cupboard and Balloons (数学)
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 数学+预处理
- Codeforces Round #285 (Div. 1)B. Misha and Permutations Summation(数学+数据结构打脸)
- Codeforces Round #144 (Div. 1), problem: (B) Table DP 组合数学
- Codeforces Round #362 (Div. 1) C PLEASE(组合数学,指数循环节)