当前位置:网站首页>动态规划【四】(计数类dp)例题:整数划分
动态规划【四】(计数类dp)例题:整数划分
2022-06-27 11:35:00 【吴雨4】
例题:整数划分
一个正整数 nn 可以表示成若干个正整数之和,形如:n=n1+n2+…+nkn=n1+n2+…+nk,其中 n1≥n2≥…≥nk,k≥1n1≥n2≥…≥nk,k≥1。
我们将这样的一种表示称为正整数 n 的一种划分。
现在给定一个正整数 n,请你求出 n 共有多少种不同的划分方法。
输入格式
共一行,包含一个整数 n。
输出格式
共一行,包含一个整数,表示总划分数量。
由于答案可能很大,输出结果请对 1e9+7 取模。
数据范围
1≤n≤10001≤n≤1000
输入样例:
5
输出样例:
7
思路:可以类似理解成完全背包,有n个物品,价值分别为1,2,3…n, 体积也分别为1,2,3…n,现在背包的容量为n,问恰好装满背包的方案数(注意每个物品可以有无数个)。

所以状态转移方程为:
f[i][j]=f[i-1][j]+f[i-1][j-i]+f[i-1][j-2*i]+....f[i-1][j-i*s]
优化一下:
f[i][j-i]= f[i-1][j-i]+f[i-1][j-2*i]+....f[i-1][j-i*s]
注意这里没有少一项,f[i][j-i]的意思是背包容量为j-i,物品数量还是为1,2,3…i,所以不可能背包容量有j-i*(s+1)的情况。
由上也可以得到优化后的状态转移方程:
f[i][j]=f[i-1][j]+f[i][j-i]
再次一维优化一下:
f [j]=f [j]+f [j-i]
代码为:
#include<bits/stdc++.h>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdio.h>
#include<iomanip>
using namespace std;
typedef long long ll;
const int N=1010;
const int mod=1e9+7;
ll n,f[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
f[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
f[j]=(f[j]+f[j-i])%mod;
}
cout<<f[n]<<endl;
return 0;
}
拓展:
y总还讲了另外一个dp思想,我直接惊奇先生,太牛了。

代码:
#include<bits/stdc++.h>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdio.h>
#include<iomanip>
using namespace std;
typedef long long ll;
const int N=1010;
const int mod=1e9+7;
ll n,f[N][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
f[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
f[i][j]=(f[i-1][j-1]+f[i-j][j])%mod;
int ans=0;
for(int i=1;i<=n;i++) ans=(ans+f[n][i])%mod;
cout<<ans<<endl;
return 0;
}
边栏推荐
- 【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)
- 等等, 怎么使用 SetMemoryLimit?
- Xuri 3sdb, installing the original ROS
- Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
- In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
- R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
- C# wpf 实现撤销重做功能
- L'utilisation de C language 0 length Array
- 【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
猜你喜欢

C語言0長度數組的妙用

星际争霸的虫王IA退役2年搞AI,自叹不如了

Unity Shader学习(二)第一个Shader

Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022

Matlab exercises - create 50 rows and 50 columns of all zero matrix, all 1 matrix, identity matrix, diagonal matrix, and output the 135 element of the matrix.

面试突击60:什么情况会导致 MySQL 索引失效?

"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu

StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior

JSP custom tag

Jwas: a Bayesian based GWAS and GS software developed by Julia
随机推荐
Summary of qstype class usage (II)
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
Challenges of machine learning system in production
QStyle类用法总结(三)
【TcaplusDB知识库】TcaplusDB分析型文本导出介绍
【TcaplusDB知识库】TcaplusDB系统用户组介绍
杰理之IO 口中断使用注意事项【篇】
杰理之睡眠以后定时唤醒系统继续跑不复位【篇】
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
Precautions for use of IO interface interrupt of Jerry [chapter]
Nvme2.0 protocol - new features
R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
Online bidding of Oracle project management system
MQTT协议栈原理及交互流程图
i. Construction of mx6ull C language environment
Unity shader learning (II) the first shader
Jerry added an input capture channel [chapter]
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
Unity Shader学习(一)认识unity shader基本结构
JSP custom tag