整数拆分-dp问题
Integer Partition
In number theory and combinatorics, a partition of a positive integer n, also called an integer partition, is a way of writing nas a sum of positive integers.
Two sums that differ only in the order of their summands are considered the same partition. For example, 4 can be partitioned in five distinct ways:
4
3 + 1
2 + 2
2 + 1 + 1
1 + 1 + 1 + 1
The order-dependent composition 1 + 3 is the same partition as 3 + 1, while the two distinct compositions 1 + 2 + 1 and 1 + 1 + 2 represent the same partition 2 + 1 + 1.
input:4
output:5
思路:
1.不考虑新加入的数,达到总和N的的种数n1
2.考虑新加入的数,达到总和N的的种数n2
3.output=n1+n2

代码:
/**
* @param {number} number
* @return {number}
*/
export default function integerPartition(number) {
// Create partition matrix for solving this task using Dynamic Programming.
const partitionMatrix = Array(number + ).fill(null).map(() => {
return Array(number + ).fill(null);
});
for (let numberIndex = ; numberIndex <= number; numberIndex += ) {
partitionMatrix[][numberIndex] = ;
}
for (let summandIndex = ; summandIndex <= number; summandIndex += ) {
partitionMatrix[summandIndex][] = ;
} // Now let's go through other possible options of how we could form number m out of
// summands 0, 1, ..., m using Dynamic Programming approach.
for (let summandIndex = ; summandIndex <= number; summandIndex += ) {//行-被加数
for (let numberIndex = ; numberIndex <= number; numberIndex += ) {//input代表的列
if (summandIndex > numberIndex) {
// If summand number is bigger then current number itself then just it won't add
// any new ways of forming the number. Thus we may just copy the number from row above.
partitionMatrix[summandIndex][numberIndex] = partitionMatrix[summandIndex - ][numberIndex];
} else {
/*
* The number of combinations would equal to number of combinations of forming the same
* number but WITHOUT current summand number PLUS number of combinations of forming the
* <current number - current summand> number but WITH current summand.
*
* Example:
* Number of ways to form 5 using summands {0, 1, 2} would equal the SUM of:
* - number of ways to form 5 using summands {0, 1} (we've excluded summand 2)
* - number of ways to form 3 (because 5 - 2 = 3) using summands {0, 1, 2}
* (we've included summand 2)
*/
const combosWithoutSummand = partitionMatrix[summandIndex - ][numberIndex];
const combosWithSummand = partitionMatrix[summandIndex][numberIndex - summandIndex]; partitionMatrix[summandIndex][numberIndex] = combosWithoutSummand + combosWithSummand;
}
}
} return partitionMatrix[number][number];
}
整数拆分-dp问题的更多相关文章
- 整数拆分 [dp+多项式插值]
题意 $1 \leq n \leq 10^{18}$ $2 \leq m \leq 10^{18}$ $1 \leq k \leq 20$ 思路 n,m较小 首先考虑朴素的$k=1$问题: $f[i] ...
- BZOJ 2173: 整数的lqp拆分( dp )
靠着暴力+直觉搞出递推式 f(n) = ∑F(i)f(n-i) (1≤i≤n) (直接想大概也不会很复杂吧...). f(0)=0 感受一下这个递推式...因为和斐波那契有关..我们算一下f(n)+f ...
- HDU1028 (整数拆分)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- poj3181【完全背包+整数拆分】
题意: 给你一个数n,在给你一个数K,问你这个n用1-k的数去组合,有多少种组合方式. 思路: 背包重量就是n: 那么可以看出 1-k就是重物,价值是数值,重量是数值. 每个重物可以无限取,问题转化为 ...
- LeetCode 343.整数拆分 - JavaScript
题目描述:给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 题目分析 题目中"n 至少可以拆分为两个正整数的和",这个条件说 ...
- Java实现 LeetCode 343 整数拆分(动态规划入门经典)
343. 整数拆分 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 示例 1: 输入: 2 输出: 1 解释: 2 = 1 + 1, 1 × ...
- HDU 4651 Partition(整数拆分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651 题意:给出n.求其整数拆分的方案数. i64 f[N]; void init(){ f[0 ...
- LightOJ 1336 Sigma Function(数论 整数拆分推论)
--->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. ...
- LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)
分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...
随机推荐
- leetcode中二分查找的具体应用
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- numpy(二)
1.集合操作 包含去重,交,并,差集操作 2.排序.搜索和计数 sort,where,argmin,argmax,count_nonzero,argwhere 3.线性代数 np.linalg库,包含 ...
- SEO优化技巧
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- Python笔记_第三篇_面向对象_2.构造函数和析构函数(含self说明)
1. 构造函数: 为什么要有构造函数? 打一个比方:类的创建就是好比你创建了好了一种格式的房间,你租给上一个住户的后,里面会对方很多“垃圾”和不规则的物品摆放.构造函数就是下一个住户再使用的时候进行物 ...
- CodeForces - 697B
这道题看见就觉得是道水题,想着随便写写就能A了,然后就开始上手直接模拟,然后就被数据打脸了. 后面就困了一个多小时,各种改,最后还是看了题解发现了scanf的多种用法. 题目大概意思就是说: 给一个 ...
- iTOP-4412开发板-can测试工具使用文档
本文档介绍如何使用 can 工具测试 can. 给用户提供了“can_libs.rar”以及“can_tools.zip”压缩包,分别是 can 工具需要的库 文件和 can 工具二进制文件. 注意开 ...
- 《VSTO开发入门教程》配套资源下载
<VSTO开发入门教程> 刘永富 著 清华大学出版社 封面截图 购书网址 京东网 淘宝网 配套资源到如下页面寻找: https://www.cnblogs.com/ryueifu-VBA/ ...
- zabbix数据库占用磁盘空间较大的处理方法
du -h /* |sort -nr 使用此命令一步步排查发现/var/lib/mysql/zabbix/这个目录占用磁盘空间较大 发现history_log.ibd这个文件最大,达到了38G,此文 ...
- element ui 自定义异步验证
之前提到过,axios是一个异步请求,但是很多时候我们都需要同步请求,比如在element的表单验证中需要验证一个用户名是否存在的时候,异步请求好像就不太好用了.前边博客中提到过,这种情况可以用es6 ...
- navicat中执行PostgreSQL错误解决:ERROR: current transaction is aborted, commands ignored until end of transaction block
错误出现: 含有错误的查询后,选中insert语句无法执行,报错current transaction is aborted, commands ignored until end of transa ...