整数拆分-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 ...
随机推荐
- C#判断两个字符串是否相等的方法 ,还有char赋空值办法。
string str1="Test"; string str2 = "Test"; if (str1==str2) //第一种判断方式 { //第二种判断方式 ...
- Web 全栈大会:万维网之父的数据主权革命
大家好,今天我和大家分享一下由万维网之父发起的一场数据主权革命.什么叫数据主权?很容易理解,现在我们的数据是把持在巨头手里的,你的微信通讯录和聊天记录都无法导出,不管是从人权角度还是从法理角度,这些数 ...
- 漫谈设计模式(一):代理(Proxy)模式与适配器(Adapter)模式对比
1.前言 为什么要将代理模式与适配器模式放在一起来说呢?因为它们有许多的共同点,当然也有一些不同的地方.首先两者都是属于结构型模式.结构型模型是这样定义的: 结构型模式涉及到如何组合类和类以获得更大的 ...
- 17.3.13--python编码问题
1----字符编码: 字符编码(英语:Character encoding).字集码是把字符集中的字符编码为指定集合中某一对象(例如:比特模式.自然数串行.8位组或者电脉冲),以便文本在计算机中存储和 ...
- C/C++ memcpy函数的用法
功能 memcpy指的是c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中 头文件 所在头文件 <s ...
- 通过if语句实现for循环的提前结束
/************************************************************************* > File Name: mybreakin ...
- Debian8.8下的VIM的配置文件
传动们:http://blog.csdn.net/gatieme/article/details/43883261?spm=5176.100239.blogcont47532.3.yXiEuB 感觉挺 ...
- ios 获取app版本号
let infoDictionary = Bundle.main.infoDictionary!let appversion = infoDictionary["CFBundleShortV ...
- Maven中settings.xml文件各标签含义
原文地址:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension settings.x ...
- servletHomeWork
2. http全称是什么? 超文本传输协议(HTTP, HyperText Transfer Protocol)是互联网上应用为最广泛的一种网络协议. 3.http协议是无状态的协议是什么意思?请说明 ...