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 ...
随机推荐
- 【汇编语言】Doxbox 0.74 修改窗口大小
1.打开Doxbox安装路径,找到DOXBox 0.74-2 Option.bat,双击打开. 2.找到windowresolution和output,将其值修改为下图中的值. 注意:图中,1280x ...
- MUI初学1
1.1)官网: http://www.dcloud.io/ 2)案例: http://www.dcloud.io/case/#group-1 3)文档:http://dev.dcloud.net.cn ...
- const与#define相比有什么不同?
C++语言可以用const定义常量,也可以用#define定义常量,但是前者比后者有更多的优点:● const常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查,而对后者只进行字符 ...
- php base64图片保存
function base64_image_content($base64_image_content,$path){ //匹配出图片的格式 if (preg_match('/^(data:\s*im ...
- SpringBoot图片上传(四) 一个input上传N张图,支持各种类型
简单介绍:需求上让实现,图片上传,并且可以一次上传9张图,图片格式还有要求,网上找了一个测试了下,好用,不过也得改,仅仅是实现了功能,其他不尽合理的地方,还需自己打磨. 代码: //html<d ...
- Application Security Per-Engagement
1. an SQLi vulnerability will allow you to do the following query the database using select statem ...
- Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
前面写的一篇Monitor监控有缺陷, 这篇文章使用Jmeter4.0+的版本, 使用插件SSHMon Samples Collector来做资源监控 1. 官网下载插件: plugins-manag ...
- powershell 激活WIN10
1.以win10专业版为例,鼠标右键点击开始图标,选择[windows powershell(管理员)],或者命令提示符管理员:2.打开命令窗口,复制这个命令slmgr /ipk W269N-WFGW ...
- [C#.Net]全局钩子实现USB扫码枪无焦点状态下扫入
https://www.cnblogs.com/masonlu/p/10105135.html
- json server服务器
json文件可以理解为数据库 一.json-server快速搭建RESTAPI 安装: sudo cnpm install -g json-server 启动(使用): json-server指向js ...