Codeforces 785 D.Anton and School - 2(组合数处理)
[Codeforces 785 D.Anton and School - 2](http://codeforces.com/problemset/problem/785/D)
题目大意:从一串由“(”,“)”组成的字符串中,找出有多少个子序列满足:序列长度为偶数,且前n/2个为“(”,后n/2个为“)”;
思路:枚举每一个左括号,则以该左括号为左右分界的子序列个数为∑C(L-1,i)C(R,i+1)(其中L为该左括号向左的左括号数,R为该左括号向右的右括号数,i从0累加到L-1)。而∑C(L-1,i)C(R,i+1)=∑C(L-1,L-1-i)C(R,i+1)=C(L+R-1,L)。
组合数预处理:
```C++
const int maxn=2e5+10,mod=1e9+7;
ll jc[maxn],inv[maxn];
ll kpow(ll a,ll x)
{
ll res=1;
while (x)
{
if (x&1)
res=res*a%mod;
a=a*a%mod;
x>>=1;
}
return res;
}
void init()
{
jc[0]=1;
for (int i=1;i=0;--i)
{
inv[i]=inv[i+1]*(i+1)%mod;;
}
}
inline ll C(ll n,ll m)
{
if (m>n)
return 0;
return mul(mul(jc[n],inv[m]),inv[n-m]);
}
```
解题代码:
```C++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair P;
typedef map M;
typedef queue Q;
typedef set S;
typedef vector V;
const int maxn=2e5+10,mod=1e9+7;
inline ll add(ll a,ll b)
{
a+=b;
if (a>=mod)
a-=mod;
if (a>=1;
}
return res;
}
void init()
{
jc[0]=1;
for (int i=1;i=0;--i)
{
inv[i]=inv[i+1]*(i+1)%mod;;
}
}
inline ll C(ll n,ll m)
{
if (m>n)
return 0;
return mul(mul(jc[n],inv[m]),inv[n-m]);
}
char s[maxn];
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
int i,j,m,n,k,l=0,r=0;
cin>>s;
n=strlen(s);
init();
for (i=0;i<n;++i)
{
r+=s[i]')';
}
ll ans=0;
for (i=0;i<n;++i)
{
l+=s[i]'(';
r-=s[i]')';
if (s[i]'(')
ans=add(ans,C(l+r-1,l));}
cout<<ans;
return 0;
}
</font>
Codeforces 785 D.Anton and School - 2(组合数处理)的更多相关文章
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- Codeforces 785 D. Anton and School - 2
题目链接:http://codeforces.com/contest/785/problem/D 我们可以枚举分界点,易知分界点左边和右边分别有多少个左括号和右括号,为了不计算重复我们强制要求选择分界 ...
- CodeForces 785 D Anton and School - 2 范德蒙恒等式
Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include ...
- Codeforces 785 - A/B/C/D/E - (Undone)
链接:https://codeforces.com/contest/785 A - Anton and Polyhedrons #include<bits/stdc++.h> using ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 785C】Anton and Fairy Tale
[题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...
- 【codeforces 785B】Anton and Classes
[题目链接]:http://codeforces.com/contest/785/problem/B [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...
随机推荐
- SSD性能测试
Tested by CrystalDiskMark 7 * MB/s = 1,000,000 bytes/s [SATA/600 = 600,000,000 bytes/s]* KB = 1000 b ...
- Flink的时间类型和watermark机制
一FlinkTime类型 有3类时间,分别是数据本身的产生时间.进入Flink系统的时间和被处理的时间,在Flink系统中的数据可以有三种时间属性: Event Time 是每条数据在其生产设备上发生 ...
- PMP - 控制图(质量控制工具)
这里用一个具体例子来解释什么是控制图. 假设现在工厂需要生成直径为1000mm (允许偏差为振幅4个标准差,即:±4mm)的螺丝钉.我们要如何保证生成过程的质量呢? 这时候我们应该对生成出来的产品进行 ...
- centos7安装配置NFS文件共享存储
一,环境介绍 本实验使用了两台centos7虚拟机,其中 服务器:192.168.1.188 客户端:192.168.1.189 二,实验步骤 192.168.1.1 ...
- 百度URL链接中文转码
百度搜索链接规则为: http://www.baidu.com/s?wd=[搜索词目]&cl=3 有多个搜索词通过加号进行链接: http://www.baidu.com/s?wd=keywo ...
- js大神成长指路
随着我的成长,我的兴趣也在不断地分化,他们似乎都是不相关的领域.我喜欢数学就像我喜欢历史一样.我的目标是成为一个多才多艺的人——一个学者——在许多领域都能成为杰出的人才.这是一项艰巨的任务.突然间,我 ...
- & 位运算总结
一.& 与 a & -a : 可以计算出 a 的二进制形式的第一个 1 出现的位置. eg: 6 & -6 = 0110 & 1010 = 0010
- 通过Callable接口实现多线程
一.通过Callable接口实现多线程 c.实现Callable重写call方法 实现Callable和实现Runnable类似,但是功能更强大,具体表现在 a.可以在任务结束后提供一个返回值,Run ...
- python之文件 I/O
打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写到标准输出如下: >>> prin ...
- react+antd引入 阿里图标
import iconfont from '../../../../assets/fonts/iconfont.js' const Iconfont = Icon.createFromIconfont ...