Balanced Subsequences
Balanced Subsequences
题意
给定 \(n,m,k\),求有多少个由 \(n\) 个 (,\(m\) 个 ) 组成的序列满足最长的合法括号子序列的长度恰为 \(2k\)。
对 \(10^9+7\) 取模,\(n,m,k\leq 2000\)。
思路
注意子序列不一定连续。
恰好最长合法括号子序列长度为 \(2k\),那么废掉的 ) 个数为 \(m-k\)。
恰好的方案数 \(f_k\) 不好求,我们可以求 \(g_k\) 表示长度至少为 \(2k\) 的方案数。
( 表示向上走,) 表示向下走,\(g_k\) 即为从 \((0,0)\) 走到 \((n+m,n-m)\),且废掉的右括号数 \(-y\le m-k\),即不超过直线 \(y=k-m\)。
\(g_k=\binom{n+m}{n}-\binom{n+m}{k-1}\)。
\(f_k=g_k-g_{k+1}=\binom{n+m}{k}-\binom{n+m}{k-1}\)。
注意判无解 \(k> \min(n,m)\)。
时间复杂度 \(O(n+m)\)。
Code
#include<bits/stdc++.h>
#define sf scanf
#define pf printf
using namespace std;
typedef long long ll;
const int N=2e3+5,mxx=N<<2,mod=1e9+7;
int t;
ll jc[mxx+5],inv[mxx+5];
ll ksm(ll a,ll b){
ll s=1;
while(b){
if(b&1){
s=s*a%mod;
}
a=a*a%mod;
b>>=1;
}
return s;
}
void init(){
jc[0]=1;
for(int i=1;i<=mxx;i++){
jc[i]=jc[i-1]*i%mod;
}
inv[mxx]=ksm(jc[mxx],mod-2);
for(int i=mxx-1;i>=0;i--){
inv[i]=inv[i+1]*(i+1)%mod;
}
}
int n,m,k;
ll C(ll n,ll m){
if(m>n) return 0;
return jc[n]*inv[n-m]%mod*inv[m]%mod;
}
int main(){
init();
sf("%d",&t);
while(t--){
sf("%d%d%d",&n,&m,&k);
if(k>min(n,m)) {
pf("0\n");
continue;
}
pf("%lld\n",(C(n+m,k)-C(n+m,k-1)+mod)%mod);
}
}
Balanced Subsequences的更多相关文章
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree
Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
随机推荐
- 使用ETLCloud实现MySQL数据库与StarRocks数据库同步
在现代数据架构中,数据同步是保证数据一致性和分析准确性的关键步骤之一.本文将介绍如何利用ETLCloud技术实现MySQL数据库与StarRocks数仓数据库的高效数据同步,以及其在数据管理和分析中的 ...
- SciTech-BigDataAIML-Jupyter- 扩展Jupyter 的 Notebook + LSP(语言服务) 的文档 Extending the Notebook
https://jupyterlab-lsp.readthedocs.io/en/latest/Installation.html https://github.com/jupyter-lsp/jup ...
- Ubuntu22.04在virtualbox上安装并配置远程
本文介绍使用virtualbox工具安装Ubuntu虚拟机,笔者选择使用22.04,理论上所有版本步骤都是一样. 同时安装virtualbox增强功能,Ubuntu与Windows使用粘贴.拖拽.自动 ...
- 解决IntelliJ IDEA在笔记本屏幕中部分界面显示不全被遮挡的问题
由于最近是work from home,笔记本分辨率高,缩放率高,但是屏幕小,并且没有外接的显示器,所以当我用IDEA社区版在笔记本上想要配置一个关于"Smart Tomcat"的 ...
- FFT 学习笔记
FFT 学习笔记 \(\mathbf{Preview}\) 前置知识. 多项式表示法 系数表示法:就是正常的多项式表示方法.\(f(x) = \sum\limits_{i = 0}^{n - 1} a ...
- 基于Astro开发的Fuwari静态博客模版配置CICD流程
基于Astro开发的Fuwari静态博客模版配置CICD流程 前言 同往常一样,上班摸鱼的时候,就喜欢逛逛随机逛逛别人的博客,然后有一个博客的主题让我有点心动. 我自己也是部署了博客的,我目前的博客网 ...
- 餐饮服务与软件创新的融合:解析海底捞 APP 的 Flutter 鸿蒙开发之路
在鸿蒙生态快速发展的背景下,作为餐饮行业数字化先锋的海底捞,其鸿蒙版应用集成在线排队.点餐等核心功能,覆盖全国超千家门店.自2024年4月海底捞与华为达成合作意愿, 6月底全面启动开发工作,9月实现首 ...
- Java7_8中的HashMap深挖
Java7 HashMap HashMap 是最简单的,一来我们非常熟悉,二来就是它不支持并发操作,所以源码也非常简单. 大方向上,HashMap 里面是一个数组,然后数组中每个元素是一个单向链表. ...
- vue3封装王编辑器组件
一.定义组件 <template> <div style="border: 1px solid #ccc"> <Toolbar style=" ...
- 深度解析ISO 50007-结合MyEMS开源系统的能源服务技术实践
在全球能源转型的背景下,如何通过技术手段提升能源服务质量并满足ISO 50007标准?MyEMS开源能源管理系统为实现这一目标提供了高效工具.本文将从技术细节到实战案例,解析如何通过MyEMS落地IS ...