Codeforces Round #404 (Div. 2) D. Anton and School - 2 前缀的后缀、 范德蒙恒等式、容斥
2017-03-16 01:44
721 查看
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.
Source
Codeforces Round #404 (Div. 2)
My Solution
题意:给出一个只有"("和“)"的字符串,为有多少个子序列,它的长度为len,则左边len/2个字符为”("右边len/2个字符为")",问这样的子序列有多少个。
前缀的后缀、 范德蒙恒等式、容斥
子串为 前缀的后缀,这里是子序列,所以s[i]为必取,作为序列的最后一个元素,然后前面的"("为选取,
所以可以预处理出suml和sumr,分表表示i的左边有多少个"(", i的右边有多少个")".
然后对于每个“(",必选这一个"(",然后i之前的(进行排列组合,选j个"(",就在sumr[i+1]里选j个")"。
所以就是sigma{C(a, j) * C(b, j)},然后这里恰好有个叫做范德蒙恒等式的公式,
范德蒙恒等式的证明
[组合数]求组合数的几种方法总结
然后根据上面所描述的方法,每次i是必选的,然后进行排列组合。
ans += C(suml[i] + sumr[i+1, suml[i]) - C(suml[i-1] + sumr[i+1], suml[i-1]);
复杂度 O(nlogn)
Thank you!
------from ProLights
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.
Source
Codeforces Round #404 (Div. 2)
My Solution
题意:给出一个只有"("和“)"的字符串,为有多少个子序列,它的长度为len,则左边len/2个字符为”("右边len/2个字符为")",问这样的子序列有多少个。
前缀的后缀、 范德蒙恒等式、容斥
子串为 前缀的后缀,这里是子序列,所以s[i]为必取,作为序列的最后一个元素,然后前面的"("为选取,
所以可以预处理出suml和sumr,分表表示i的左边有多少个"(", i的右边有多少个")".
然后对于每个“(",必选这一个"(",然后i之前的(进行排列组合,选j个"(",就在sumr[i+1]里选j个")"。
所以就是sigma{C(a, j) * C(b, j)},然后这里恰好有个叫做范德蒙恒等式的公式,
范德蒙恒等式的证明
[组合数]求组合数的几种方法总结
然后根据上面所描述的方法,每次i是必选的,然后进行排列组合。
ans += C(suml[i] + sumr[i+1, suml[i]) - C(suml[i-1] + sumr[i+1], suml[i-1]);
复杂度 O(nlogn)
#include <iostream> #include <cstdio> #include <string> using namespace std; typedef long long LL; const int MAXN = 2e5 + 8; const LL MOD = 1e9 + 7; LL suml[MAXN], sumr[MAXN], fac[MAXN]; string s; inline LL mod(LL a) { return a - a / MOD * MOD; } inline LL pow_mod(LL a, LL i) { if(i == 0) return mod(1); LL t = pow_mod(a, i>>1); t = mod(t * t); if(i & 1) t = mod(t * a); return t; } inline LL get(LL a,LL b) { return mod(fac[a+b] * pow_mod(mod(fac[a] * fac[b]), MOD - 2)); } int main() { #ifdef LOCAL freopen("d.txt", "r", stdin); //freopen("d.out", "w", stdout); int T = 4; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); cin >> s; int sz = s.size(), i; fac[0] = 1; for(i = 1; i <= sz; i++) fac[i] = mod(fac[i-1] * i); for(i = 0; i < sz; i++){ if(i == 0){ suml[i] = 0; if(s[i] == '(') suml[i] += 1; } else{ suml[i] = suml[i-1]; if(s[i] == '(') suml[i] += 1; } } sumr[sz] = 0; for(i = sz - 1; i >= 0; i--){ sumr[i] = sumr[i+1]; if(s[i] == ')') sumr[i] += 1; } LL ans = 0; for(i = 0; i < sz; i++){ if(s[i] == '('){ ans = mod(ans + get(suml[i], sumr[i+1]) - get(suml[i-1], sumr[i+1]) ); } } while(ans < 0) ans += MOD; cout << ans << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
Thank you!
------from ProLights
相关文章推荐
- Codeforces Round #404 (Div. 2) D. Anton and School - 2(组合数学)
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 排列组合好题
- codeforces round 404 div2 D Anton and School - 2 组合数学
- Codeforces Round #404 (Div. 2) D. Anton and School - 2
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
- Codeforces Round #379 (Div. 2) F. Anton and School
- Codeforces Round #379 (Div. 2) F. Anton and School
- 【Codeforces Round 354 (Div 2)C】【前缀和二分or双指针】Vasya and String ab序列最多改变k位置的最长同字符子串长度
- 【Codeforces Round 370 (Div 2) D】【前缀和打标记】Memory and Scores 双人随机数值增减K A比B多的方案数
- Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心
- [排列组合]Codeforces Round #324 (Div. 2)E - Anton and Ira
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
- Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
- 【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和
- 【Codeforces Round 333 (Div 2)E】【期望DP概率做法 树状数组转前缀和】Kleofáš and the n-thlon n场比赛m个人获得总名次的期望
- Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)
- Codeforces Round #379 (Div. 2)-D. Anton and Chess
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀
- Codeforces Round #253 (Div. 2) A. Anton and Letters