【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2
http://codeforces.com/blog/entry/50996
官方题解讲得很明白,在这里我复述一下。
枚举每个左括号,考虑计算一定包含其的简单括号序列的个数,只考虑其及其左侧的左括号,以及其右侧的右括号。最后答案就是其之和。
可以将其提取出来这样((((((())),红色为当前左括号。设有x个左,y个右
要注意,这个答案为C(x+y-1,x),来证明。
我们只需证明,这个答案与长度为x+y-1的,包含x个1的零一序列的种类数相等即可。
随便写一个这样的零一序列,长度为x+y,但当前的左括号的位置一定是零。
比如110100111,除了红色位置以外,设有z个右括号对应1,那么有(x-z)个左括号对应1,那么有(x-1-(x-z))=z-1个左括号对应0。
恰好枚举遍了包含1,2,...,y个右括号,0,1,...,y-1个左括号(不含当前位置)的所有情况。于是显然这个答案是对的。
具体算的时候,预处理一下阶乘,暴力搞一下逆元就行了。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
#define MOD 1000000007ll
ll Quick_Pow(ll a,ll p)
{
if(!p){
return 1;
}
ll ans=Quick_Pow(a,p>>1);
ans=ans*ans%MOD;
if((p&1)==1){
ans=ans*a%MOD;
}
return ans;
}
int n;
char a[200010];
ll jc[200010],ans,sumr;
int main(){
// freopen("d.in","r",stdin);
scanf("%s",a+1);
n=strlen(a+1);
jc[0]=1ll;
for(int i=1;i<=n;++i){
jc[i]=(jc[i-1]*(ll)i)%MOD;
}
for(int i=1;i<=n;++i){
sumr+=(a[i]==')');
}
int x=0,y=0;
for(int i=1;i<=n;++i){
if(a[i]=='('){
++x;
}
else{
++y;
}
if(sumr-y>=1 && a[i]=='('){
ans=(ans+(((jc[x+sumr-y-1]*Quick_Pow(jc[x],MOD-2ll))%MOD)*Quick_Pow(jc[sumr-y-1],MOD-2ll))%MOD)%MOD;
}
}
cout<<ans<<endl;
return 0;
}
【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2的更多相关文章
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学
D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分
C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...
- Codeforces Round #404 (Div. 2) B. Anton and Classes 水题
B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...
- Codeforces Round #404 (Div. 2) A - Anton and Polyhedrons 水题
A - Anton and Polyhedrons 题目连接: http://codeforces.com/contest/785/problem/A Description Anton's favo ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2
题目链接 转自 给你一个字符串问你能构造多少RSBS. #include<bits/stdc++.h> #define LL long long #define fi first #def ...
- 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
随机推荐
- Less & Sass
CSS不是一种编程语言.它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,它没有变量,也没有条件语句,只是一行行单纯的描述.有人就开始为CSS加入编程元素,这被叫做"C ...
- Python 用ctypes观察Python对象的内存结构 -- (转)
!!!强烈推荐的好文章!!! 对象的两个基本属性 Python所有对象结构体中的头两个字段都是相同的: refcnt:对象的引用次数,若引用次数为0则表示此对象可以被垃圾回收了. typeid:指向描 ...
- SpringCloud Feign重试详解
摘要: 今天在生产环境发生了数据库进程卡死的现象,除了sql因为全量更新,没加索引的原因,最主要还是我们的接口的服务器端接口出现问题了.忽视了更新接口的幂等性,以及调用方feign client的重试 ...
- Bitmap算法应用实例(转)
关于数据库查询工作,例如有如下数据库表 要想统计所有90后的程序员该怎么做呢?用一条求交集的SQL语句即可: Select count(distinct Name) as 用户数 from table ...
- socket编程中write、read和send、recv之间的区别~转载
socket编程中write.read和send.recv之间的区别 http://blog.csdn.net/petershina/article/details/7946615 一旦,我们建立 ...
- mysql之基本数据库操作(二)
环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 mysql的启动.退出.重启 # 启动 $ sudo service mysqld start # 停止 $ sud ...
- Vim中的键映射【转】
转自:http://www.cnblogs.com/softwaretesting/archive/2011/09/28/2194515.html http://www.pythonclub.org/ ...
- [ Linux ] 服务器更换硬盘
服务器型号:ThinkServer RD650操作系统:Red Hat 6.7业务用途:生产环境监控机 接到机房邮件通知,告知某台服务器硬盘告警,并提供了设备SN号和机柜位置.根据提供想相关信息找到对 ...
- C++变量类型转换
1:int转换为CString CString str; str.Format("As string: %d", int); 2:double转换为CString CString ...
- 用eclipse运行项目时怎么设置虚拟机内存大小
方法一: 打开eclipse,选择Window--Preferences...在对话框左边的树上双击Java,再双击InstalledJREs,在右边选择前面有对勾的JRE,再单击右边的“Edit”按 ...