当前位置:网站首页>一道题目初探组合数学与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;
边栏推荐
- 1259. Programmation dynamique de poignée de main disjointe
- Tcl 语言之Synopsys Tcl篇(3)(数字IC)
- Paddlenlp's UIE classification model [taking emotional propensity analysis news classification as an example] including intelligent annotation scheme)
- Detailed explanation of TCL scripting language (1)
- Building virtual private network based on softther
- [2020] [paper notes] Based on Rydberg atom——
- .NET Core 实现后台任务(定时任务)Longbow.Tasks 组件(三)
- quota的使用方法
- DP problem collection
- [onnx] the problem of dynamic input size (multi output / multi input)
猜你喜欢

小鱼送激光雷达啦 | 恰饭即抽奖第一期

基於FPGA的UART接口設計

Challenges of decentralized storage

How can zero foundation self-study software testing? Ten year test Laoniao's strongest software test learning Roadmap
![[shutter -- layout] linear layout (row and column)](/img/0e/df0f4bce73dd9785cc843adaf371d0.png)
[shutter -- layout] linear layout (row and column)

Google is improving the skin color performance in all products and practicing the concept of "image fairness"
![Multithreading [comprehensive study of graphics and text]](/img/70/8a1227b2159349cf25a85dff8f9d1c.png)
Multithreading [comprehensive study of graphics and text]

Detailed explanation: tmp1750 chip three channel linear LED driver

作为一名后台开发人员,你必须知道的两种过滤器

elk笔记25--快速体验APM
随机推荐
一、爬虫概念及基本流程
not all arguments converted during string formatting
Emgucv common function function description "suggestions collection"
Google正在改进所有产品中的肤色表现 践行“图像公平”理念
C # split usage, split split split string
elk笔记25--快速体验APM
人脸识别系统技术方案
作为一名后台开发人员,你必须知道的两种过滤器
[2018] [paper notes] graphene FET and [2] - Preparation and transfer of graphene
图学习总结
[2020] [paper notes] optically controlled spectral ratio adjustable y based on two-dimensional photonic crystal——
Cell array processing
TCL脚本语言详解(1)
Ros2 self study notes: rviz visualization tool
MQ [messagequeue graphic explanation and four MQ comparisons]
How can win11 add 3D effects to pictures? Win11 method of adding picture 3D effect
Rapid establishment of devstack cloud computing platform
C#Split的用法,Split分割字符串
Know two things: how does redis realize inventory deduction and prevent oversold?
How can zero foundation self-study software testing? Ten year test Laoniao's strongest software test learning Roadmap