当前位置:网站首页>一道题目初探组合数学与DP关系,可重集组合公式推导
一道题目初探组合数学与DP关系,可重集组合公式推导
2022-07-23 17:25:00 【lovesickman】
4002. 构造数组 (揭示了DP的本质是组合数学)
现在需要构造一对数组 ( a , b ) (a,b) (a,b),要求:
- 数组 a a a 和数组 b b b 的长度都为 m m m。
- 两个数组中的元素的取值范围都是 [ 1 , n ] [1,n] [1,n]。
- ∀ i ∈ [ 1 , m ] ∀i∈[1,m] ∀i∈[1,m], a i ≤ b i ai≤bi ai≤bi。
- 数组 a a a 中元素非严格单调递增。
- 数组 b b b 中元素非严格单调递减。
请问,共能构造出多少对满足条件的数组?
输出对 1 0 9 + 7 10^9+7 109+7 取模后的结果。
乍一看条件非常的多,感觉很难。
方法:数形结合,乘法原理(分步骤考虑)
只要在纸上随便画下 a a a 和 b b b 就能发现问题可以转化成:
- 从 n n n 个数中(每个数可以重复选)选 2 ∗ m 2*m 2∗m 个数,对这些数排列,大的一半给 b b b ,另一半给 a a a 。
- 分析 a a a 中的 m m m 个数,有多少种分配方案?
解答 2 :发现给定的 m m m 个数确定之后,方案是唯一的。
所以问题等价于 1 的方案数(经典可重集组合问题),答案是 C ( 2 m + n − 1 , n − 1 ) C(2m+n-1,n-1) C(2m+n−1,n−1)
数学推导:(思路,已知 C ( n , m ) C(n,m) C(n,m) 表示从 n n n 个不同的数中取 m m m 个的方案数)。
以下为数学中的模板思维套路:
- 设 x i x_i xi 表示第 i i i 个数取多少数。有 ∑ i = 1 n x i = 2 ∗ m , x i ≥ 0 \sum_{i=1}^{n} x_i =2*m,x_i \ge0 ∑i=1nxi=2∗m,xi≥0
- 设 x i ′ = x i + 1 x'_i = x_i +1 xi′=xi+1 有 ∑ i = 1 n x i = 2 ∗ m + n , x i > 0 \sum_{i=1}^{n} x_i =2*m+n,x_i > 0 ∑i=1nxi=2∗m+n,xi>0
- 问题转化为给定 2 ∗ m + n 2*m+n 2∗m+n 个小球,用 n − 1 n-1 n−1 个隔板将所有小球分成 n n n 部分,每部分小球的数量必须 大于 0 0 0 。因为 x i > 0 x_i>0 xi>0 ,所以隔板有 2 ∗ m + n − 1 2*m+n-1 2∗m+n−1 个位置,又因为每个隔板要不同,所以可以用 C ( 2 m + n − 1 , n − 1 ) C(2m+n-1,n-1) C(2m+n−1,n−1) 表示。得证。
一个小TRICK C ( 2 m + n − 1 , n − 1 ) C(2m+n-1,n-1) C(2m+n−1,n−1) = C ( 2 m + n − 1 , 2 m ) C(2m+n-1,2m) C(2m+n−1,2m) 哪个数据范围小用哪个
组合数怎么求? C ( n , m ) = C ( n − 1 , m − 1 ) + C ( n − 1 , m ) C(n,m) = C(n-1,m-1) + C(n-1,m) C(n,m)=C(n−1,m−1)+C(n−1,m) 这不就是 0/1背包的推导方式吗?
所以: C ( 2 m + n − 1 , n − 1 ) C(2m+n-1,n-1) C(2m+n−1,n−1) 可以用 0 / 1 0/1 0/1 背包。 2 m + n − 1 2m+n-1 2m+n−1 是背包的体积,每个元素的体积是 1 1 1 。
原来 DP 可以求方案数是因为 DP 的底层逻辑是组合数学,组合数学中的乘法原理加法原理可以求方案数。
那么一个明显的思路,可重集求方案数是不是可以直接用完全背包呢?答案是显然的!
以下给出两种背包求同一问题的代码。
0/1背包
f[0]=1;
for(int i=1;i<=2*m+n-1;i++){
for(int j=2*m;j>=1;j--){
f[j]= ((f[j] + f[j-1]) + M )%M;
}
}
cout<<f[2*m]<<endl;
完全背包
f[0]=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=2*m;j++){
f[j]= ((f[j] + f[j-1]) + M )%M;
}
}
cout<<f[2*m]<<endl;
边栏推荐
- 二叉树高度 [log2n]+1与log2(n+1)是否相等
- SQL statement exercise
- 作为一名后台开发人员,你必须知道的两种过滤器
- Learn and understand Architecture Design from business development
- Emgucv common function function description "suggestions collection"
- 1259. 不相交的握手 動態規劃
- Tcl 语言之Synopsys Tcl篇(3)(数字IC)
- Read data from txt and convert it to Yolo format data
- Redis [2022 latest interview question]
- 1、 Reptile concept and basic process
猜你喜欢

Ros2 self study notes: rviz visualization tool

1259. Disjoint handshake dynamic programming

Google is improving the skin color performance in all products and practicing the concept of "image fairness"

界面设计四大原则

Todo fix bug tag feature and other configurations
![二叉树高度 [log2n]+1与log2(n+1)是否相等](/img/64/381376190218d5b2cdfd8b1197e8f6.png)
二叉树高度 [log2n]+1与log2(n+1)是否相等

多线程&高并发(全网最新:面试题+导图+笔记)面试手稳心不慌

LeetCode每日一题(1514. Path with Maximum Probability)

基于FPGA的SPI通讯协议实现
![[2013] [paper notes] terahertz band nano particle surface enhanced Raman——](/img/09/af80a744573ce53a0056e7124675a8.png)
[2013] [paper notes] terahertz band nano particle surface enhanced Raman——
随机推荐
FPGA基于spi的flash读写
What is the use of tampermonkey?
mysql的访问端口是什么意思_数据库端口是什么端口号
界面设计四大原则
Handwriting bind, call, apply is actually very simple
Log framework [detailed learning]
Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
[the whole process of Game Modeling and model production] create the game soldier character with ZBrush
电子元件-电阻
What content does the software test plan include and how to write it. Share test plan template
Error reporting caused by the use of responsebodyadvice interface and its solution
SQL statement exercise
.NET Core 实现后台任务(定时任务)Longbow.Tasks 组件(三)
What is the difference between preamplifier and power amplifier?
[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz
DP problem collection
Terminal终端命令(全)
【论文阅读】GETNext: Trajectory Flow Map Enhanced Transformer for Next POI Recommendation
How can win11 add 3D effects to pictures? Win11 method of adding picture 3D effect
AE 教程,如何在 After Effects 中对 Illustrator 分图层文档进行动画绘制?