题目链接: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 - [范德蒙德恒等式][快速幂+逆元]的更多相关文章

  1. bzoj 4830: [Hnoi2017]抛硬币 [范德蒙德卷积 扩展lucas]

    4830: [Hnoi2017]抛硬币 题意:A投a次硬币,B投b次硬币,a比b正面朝上次数多的方案数,模\(10^k\). \(b \le a \le b+10000 \le 10^{15}, k ...

  2. 浅谈范德蒙德(Vandermonde)方阵的逆矩阵的求法以及快速傅里叶变换(FFT)中IDFT的原理

    浅谈范德蒙德(Vandermonde)方阵的逆矩阵与拉格朗日(Lagrange)插值的关系以及快速傅里叶变换(FFT)中IDFT的原理 标签: 行列式 矩阵 线性代数 FFT 拉格朗日插值 只要稍微看 ...

  3. 【题解】幼儿园篮球题(范德蒙德卷积+斯特林+NTT)

    [题解]幼儿园篮球题(NTT+范德蒙德卷积+斯特林数) 题目就是要我们求一个式子(听说叫做超几何分布?好牛逼的名字啊) \[ \sum_{i=1}^{S}\dfrac 1 {N \choose n_i ...

  4. CodeForces 785 D Anton and School - 2 范德蒙恒等式

    Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include ...

  5. 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 ...

  6. [刷题]Codeforces 785D - Anton and School - 2

    Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...

  7. CodeForces 785D Anton and School - 2

    枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...

  8. CodeForces 785D Anton and School - 2 (组合数学)

    题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...

  9. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

随机推荐

  1. JS获取当月第一天和最后一天

    /** * 获取当前月的第一天 */function getCurrentMonthFirst(){ var date=new Date(); date.setDate(1); return date ...

  2. springboot集成elasticsearch遇到的问题

    public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String>{ Page<EsBl ...

  3. Web从入门到放弃<4>

    1,插入 如下html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  4. Docker设置http代理

    在国内由于不可描述的原因无法访问Google等网站,但是作为一枚挨踢人士,无法使用Google搜索,在使用Ctrl + C技能时是抓狂的:特别是当下Docker.Kubernetes等容器技术火热的时 ...

  5. ES进阶--01

    第2节结构化搜索_在案例中实战使用term filter来搜索数据 课程大纲 1.根据用户ID.是否隐藏.帖子ID.发帖日期来搜索帖子 (1)插入一些测试帖子数据 POST /forum/articl ...

  6. unity 使用方法

    1.Rotaion 想要设定一个实例的rotation的时候不能使用Vector3来直接设定:应改为 rotation = Quaternion.Euler (0.0f, 180.0f, 0.0f); ...

  7. Windows Internals 笔记——终止进程

    1.进程可以通过以下四种方式终止: 主线程的入口点函数返回(强烈推荐的方式) 进程中的一个线程调用ExitProcess函数(避免这种方式) 另一个进程中的线程调用TerminateProcess函数 ...

  8. echart 常用配置

    mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...

  9. [原创]Xilinx工具关联UEStudio

    UE安装目录如下: C:\Program Files (x86)\IDM Computer Solutions\UEStudio\UEStudio.exe 对于ISE工具,在Editor -> ...

  10. CentOS7 nginx启动脚本

    vi /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=fo ...