Codeforces Round #404 (Div. 2) D. Anton and School - 2 排列组合好题
2017-03-20 10:58
633 查看
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.
对每个‘(’ 存一下他前面‘(’的个数(假设是n,不包括他自己)和后面‘)’的个数(假设是m),以这个‘(’为中心的子串的数量是
有一个范德蒙恒等式
特殊的当k=n或m时
和杨辉三角式
当k=m时,上式=
当k=n+1时,上式=
两种情况的结果一样
求组合数时,因为除法会影响余数,所以要用费马小定理求分母部分的乘法逆元
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.
对每个‘(’ 存一下他前面‘(’的个数(假设是n,不包括他自己)和后面‘)’的个数(假设是m),以这个‘(’为中心的子串的数量是
有一个范德蒙恒等式
特殊的当k=n或m时
和杨辉三角式
当k=m时,上式=
当k=n+1时,上式=
两种情况的结果一样
求组合数时,因为除法会影响余数,所以要用费马小定理求分母部分的乘法逆元
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define eps 1e-9
#define PI 3.141592653589793
#define bs 1000000007#define bsize 256#define MEM(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
ll jie[200050],ni[200050];
ll poww(ll a)
{
ll p=bs-2;
ll t=a,ans=1;
while(p)
{
if(p&1)
{
ans*=t;
ans%=bs;
}
t*=t;
t%=bs;
p>>=1;
}
return ans;
}
int init()
{
jie[0]=1;
ni[0]=1;
for(ll i=1;i<=200000;i++)
{
jie[i]=(jie[i-1]*i)%bs;
ni[i]=poww(jie[i])%bs;
}
}
int le[200005],ri[200005];
int main()
{
char ch[200005];
scanf("%s",ch+1);
init();
int len=strlen(ch+1);
int i,j;
for(i=1;i<=len;i++)
{
le[i]=le[i-1];
if(ch[i]=='(')
le[i]++;
}
for(i=len;i>=1;i--)
{
ri[i]=ri[i+1];
if(ch[i]==')')
ri[i]++;
}
long long sum=0;
int n,m;
for(i=1;i<=len;i++)
{
if(ch[i]=='(')
{
n=le[i]-1;
m=ri[i];
sum+=(((jie[n+m]*ni[m-1])%bs)*ni[n+1])%bs;
sum%=bs;
}
}
printf("%lld\n",sum);
}
相关文章推荐
- codeforces round 404 div2 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 #324 (Div. 2)E - Anton and Ira
- Codeforces Round #404 (Div. 2) D. Anton and School - 2
- Codeforces Round #379 (Div. 2) F. Anton and School
- 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 324 (Div 2)E】【贪心 构造】Anton and Ira 全排列交换 最小距离成本
- Codeforces Round #379 (Div. 2) E. Anton and Tree
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
- Codeforces Round #379 (Div. 2)E. Anton and Tree
- Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心
- Codeforces Round #253 (Div. 2) A. Anton and Letters
- Codeforces Round #379 (Div. 2)-C. Anton and Making Potions
- Codeforces Round #329 (Div. 2)B. Anton and Lines
- Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟
- Codeforces Round #253 (Div. 2) A. Anton and Letters
- Codeforces Round #379 (Div. 2) D. Anton and Chess 水题