当前位置:网站首页>Dynamic programming [4] (counting class DP) example: integer partition
Dynamic programming [4] (counting class DP) example: integer partition
2022-06-27 12:01:00 【Wu Yu 4】
Example : Integer partition
link :900. Integer partition - AcWing Question bank
A positive integer nn It can be expressed as the sum of several positive integers , Form like :n=n1+n2+…+nkn=n1+n2+…+nk, among n1≥n2≥…≥nk,k≥1n1≥n2≥…≥nk,k≥1.
We call such a representation a positive integer n A division of .
Now let's give a positive integer n, Please find out n How many different division methods are there .
Input format
All in one line , Contains an integer n.
Output format
All in one line , Contains an integer , Indicates the total number of partitions .
Because the answer can be big , Please correct the output result 1e9+7 modulus .
Data range
1≤n≤10001≤n≤1000
sample input :
5
sample output :
7
Ideas : It can be interpreted as a complete backpack , Yes n Items , The values are 1,2,3…n, The volume is also 1,2,3…n, Now the capacity of the backpack is n, Ask the number of schemes that just fill the backpack ( Note that each item can have countless ).

So the state transfer equation is :
f[i][j]=f[i-1][j]+f[i-1][j-i]+f[i-1][j-2*i]+....f[i-1][j-i*s]
Optimize it :
f[i][j-i]= f[i-1][j-i]+f[i-1][j-2*i]+....f[i-1][j-i*s]
Note that there is nothing missing here ,f[i][j-i] The capacity of the backpack is j-i, Item quantity or 1,2,3…i, So it's impossible for the backpack to have j-i*(s+1) The situation of .
The optimized state transition equation can also be obtained from the above :
f[i][j]=f[i-1][j]+f[i][j-i]
One dimensional optimization again :
f [j]=f [j]+f [j-i]
The code is :
#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;
}
expand :
y There is always another one dp thought , I am directly surprised, sir , Too cattle .

Code :
#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;
}
边栏推荐
- Wait, how do I use setmemorylimit?
- [tcapulusdb knowledge base] tcapulusdb system user group introduction
- 聊聊 Go 语言与云原生技术
- 杰理之IO 口中断使用注意事项【篇】
- Peak store app imitation station development play mode explanation source code sharing
- [tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)
- QStyle类用法总结(二)
- 内存四区(栈,堆,全局,代码区)
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
- [tcapulusdb knowledge base] Introduction to tcapulusdb data structure
猜你喜欢

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

QStyle类用法总结(三)

C/s architecture

干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了

【TcaplusDB知识库】TcaplusDB常规单据介绍

MapReduce principle analysis (in-depth source code)

Redis 分布式锁15问,看看你都掌握了哪些?

Unity shader learning (I) understanding the basic structure of unity shader

MapReduce实战小案例(自定义排序、二次排序、分组、分区)

数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
随机推荐
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
R language dplyr package arrange function sorts dataframe data, sorts dataframe data through multiple data columns, specifies the first field to be sorted in descending order, and does not specify the
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, specifies the grouping parameters with the b
The DBSCAN function of FPC package in R language performs density clustering analysis on data, and the plot function visualizes the clustering graph
R language uses the polR function of mass package to construct the ordered multi classification logistic regression model, and uses the vglm function of VGAM package to test the parallelism hypothesis
The R language uses the follow up The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The labels parameter adds label information t
I.MX6ULL启动方式
In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
2022CISCN华中 Web
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
旭日3SDB,安装原版ros
R language uses the poisgof function of epidisplay package to test the goodness of fit of Poisson regression and whether there is overdispersion
Online bidding of Oracle project management system
[tcapulusdb knowledge base] tcapulusdb business data backup introduction
pull request
Wechat applet realizes five-star evaluation
[tcapulusdb knowledge base] Introduction to tcapulusdb table data caching
等等, 怎么使用 SetMemoryLimit?
Jerry's seamless looping [chapter]