Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D
题解:
首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 "(",以及右边有 $y$ 个 ")",那么就有式子如下:
① 若 $x+1 \le y$:$C_{x}^{0} C_{y}^{1} + C_{x}^{1} C_{y}^{2} + \cdots + C_{x}^{x} C_{y}^{x+1} = \sum_{i=0}^{x} C_{x}^{i} C_{y}^{i+1}$
② 若 $x+1 > y$:$C_{x}^{0} C_{y}^{1} + C_{x}^{1} C_{y}^{2} + \cdots + C_{x}^{y-1} C_{y}^{y} = \sum_{i=0}^{y-1} C_{x}^{i} C_{y}^{i+1}$
然后算一下,哦哟 $O(n^2)$ 的优秀算法,GG,想了半天也不知道咋优化,看了题解才知道是“范德蒙德恒等式”:
$\sum_{i=0}^{r} C_{m}^{i} C_{n}^{r-i} = C_{m+n}^{r}$
以及它的一个推导等式:
$\sum_{i=0}^{m} C_{m}^{i} C_{n}^{r+i} = C_{m+n}^{m+r}$
① 直接用推导等式可以得到:
$\sum_{i=0}^{x} C_{x}^{i} C_{y}^{i+1} = C_{x+y}^{x+1}$
而 ② 则用范德蒙德恒等式得到:
$\sum_{i=0}^{y-1} C_{x}^{i} C_{y}^{i+1} = \sum_{i=0}^{y-1} C_{x}^{i} C_{y}^{y-1-i} = C_{x+y}^{y-1}$
综上,就变成了:对于每个 "(",假设其左边还有 $x$ 个 "(",右边有 $y$ 个 ")",那么对于答案的贡献:
① 若 $x+1 \le y$,则为 $C_{x+y}^{x+1}$
② 若 $x+1 > y$,则为 $C_{x+y}^{y-1}$
只要预处理出阶乘和阶乘的逆元,那么每次算 $C_{n}^{r}$ 就是 $O(1)$ 的。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
const int maxn=2e5+; char s[maxn];
int n,x[maxn],y[maxn]; ll fpow(ll a,ll n)
{
ll res=, base=a%mod;
while(n)
{
if(n&) res*=base, res%=mod;
base*=base, base%=mod;
n>>=;
}
return res%mod;
}
ll inv(ll a){return fpow(a,mod-);} ll fac[maxn],fac_inv[maxn];
ll C(ll n,ll r)
{
ll res=fac[n];
res*=fac_inv[r], res%=mod;
res*=fac_inv[n-r], res%=mod;
return res;
} int main()
{
fac[]=, fac_inv[]=inv(fac[]);
for(int i=;i<maxn;i++) fac[i]=fac[i-]*i%mod, fac_inv[i]=inv(fac[i]); scanf("%s",s+), n=strlen(s+); x[]=;
for(int i=;i<=n;i++) x[i]=x[i-]+(s[i-]=='(');
y[n]=;
for(int i=n-;i>;i--) y[i]=y[i+]+(s[i+]==')');
//for(int i=1;i<=n;i++) printf("%d %d\n",x[i],y[i]); ll ans=;
for(int i=;i<=n;i++)
{
if(s[i]!='(') continue;
if(y[i]<=) continue;
if(x[i]+<=y[i])
ans+=C(x[i]+y[i],x[i]+), ans%=mod;
else
ans+=C(x[i]+y[i],y[i]-), ans%=mod;
}
cout<<ans<<endl;
}
Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]的更多相关文章
- bzoj 4830: [Hnoi2017]抛硬币 [范德蒙德卷积 扩展lucas]
4830: [Hnoi2017]抛硬币 题意:A投a次硬币,B投b次硬币,a比b正面朝上次数多的方案数,模\(10^k\). \(b \le a \le b+10000 \le 10^{15}, k ...
- 浅谈范德蒙德(Vandermonde)方阵的逆矩阵的求法以及快速傅里叶变换(FFT)中IDFT的原理
浅谈范德蒙德(Vandermonde)方阵的逆矩阵与拉格朗日(Lagrange)插值的关系以及快速傅里叶变换(FFT)中IDFT的原理 标签: 行列式 矩阵 线性代数 FFT 拉格朗日插值 只要稍微看 ...
- 【题解】幼儿园篮球题(范德蒙德卷积+斯特林+NTT)
[题解]幼儿园篮球题(NTT+范德蒙德卷积+斯特林数) 题目就是要我们求一个式子(听说叫做超几何分布?好牛逼的名字啊) \[ \sum_{i=1}^{S}\dfrac 1 {N \choose n_i ...
- CodeForces 785 D Anton and School - 2 范德蒙恒等式
Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include ...
- Codeforces 785D Anton and School - 2 (组合数相关公式+逆元)
D. Anton and School - 2 time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
- CodeForces 785D Anton and School - 2
枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...
- CodeForces 785D Anton and School - 2 (组合数学)
题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
随机推荐
- linux文件系统初始化过程(3)---加载initrd(上)
一.目的 本文主要讲述linux3.10文件系统初始化过程的第二阶段:加载initrd. initrd是一个临时文件系统,由bootload负责加载到内存中,里面包含了基本的可执行程序和驱动程序.在l ...
- 【原创】大叔问题定位分享(4)Kafka集群broker节点从zookeeper上消失
kafka_2.8.0-0.8.1 一 现象 生产环境一组kafka集群经常发生问题,现象是kafka在zookeeper上的broker节点消失,此时kafka进程和端口都在,然后每个broker都 ...
- Ubuntu18.04 LTS 安装部署golang最新版本1.10
1 步骤 //1 直接安装golang-go 目前最新版本是1.10 sudo apt-get install golang-go //2 向/etc/profile追加以下代码 sudo vim / ...
- docker简单介绍---网络端口管理
一.查看docker支持的网络类型 docker network ls bridge:容器使用虚拟交换机的进行通信 host:使用宿主机的网络 none:只给容器分配一个lo的网卡,无法和外界进行通信 ...
- php curl使用
- background-image属性
background-image 属性 实例,设置body元素的背景图像: body { background-image: url('paper.gif'); background-color: # ...
- python运算符——比较运算符
比较运算符的运算结果会得到一个bool类型,也就是逻辑判定,要么是真True,要不就是False 大于“>” 小于“<” 不说了,看看不等于,用“!=”表示.大于等于“>=”和小 ...
- pyqt pyside 窗口自动调整大小
pyqt pyside 窗口自动调整大小 在QTimer中一直调整 def initTimer(self): self.resizeTimer = QtCore.QTimer(self) self.r ...
- elk安装时最常见的报错
1.在启动kibana的时候报一下错误 max file descriptors [4096] for elasticsearch process likely too low, increase t ...
- System优化
从系统方面考虑,性能通常取决于connection的连接效率和Integration Service所在机器的负荷程度,常见的原因有: 多用户同时使用 不同的网络协议 网络上有多个路由及转换 源和目标 ...