当前位置:网站首页>最基础01/完全背包
最基础01/完全背包
2022-07-30 11:58:00 【Codiplay】
给定
个正整数
从中取出若干个数,使它们的和为 M,求有多少种选择方案。
因为每个数只能用一次,所以是0|1背包。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 110;
int f[N][11100];
int a[N];
int main(){
//std::ios::sync_with_stdio(false);
//std::cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
f[0][0] = 1;
for (int i = 1; i <= n; i ++ ) {
for (int j = 0; j <= m; j ++ ) {//j必须从0开始,初始化简单,而且符合定义
f[i][j] = f[i - 1][j]; //不选
if(a[i] <= j) f[i][j] += f[i - 1][j - a[i]];//能选的前提下选
}
}
cout << f[n][m] << '\n';
return 0;
}因为阶段i都是由阶段i-1更新而来,0/1的经典优化——倒叙循环。保证用i-1更新i阶段
将“阶段”隐藏起来了,f[i]表示总和为i
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 11000;
int f[N];
int a[N];
int main(){
//std::ios::sync_with_stdio(false);
//std::cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
f[0] = 1;
for (int i = 1; i <= n; i ++ ) {
for (int j = m; j >= a[i]; j -- ) {
f[j] += f[j - a[i]];
}
}
cout << f[m] << '\n';
return 0;
}
从1到n可以取无数次,112和211是一种方案,完全背包
完全背包正序。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 11000;
const int mod = 1e9 + 7;
int f[N];
int main(){
//std::ios::sync_with_stdio(false);
//std::cin.tie(nullptr);
int n;
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] << '\n';
return 0;
}边栏推荐
- Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
- SCM engineers written questions induction summary
- 基于加权灰色关联投影的Bagging-Blending多模型融合短期电力负荷预测
- saltstack学习3模块
- Concepts of cloud-native applications and 15 characteristics of cloud-native applications
- 流水线上的农民:我在工厂种蔬菜
- Summary of text alignment, line height, space, etc.
- Homework 7.29 correlation function directory and file attributes related functions
- 电脑奔溃的时候,到底发生了什么?
- JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
猜你喜欢
![[BJDCTF2020]Cookie is so stable-1|SSTI注入](/img/48/34955bbe3460ef09a5b8213c7cc161.png)
[BJDCTF2020]Cookie is so stable-1|SSTI注入

EA中的业务对象和业务实体你分得清吗?

Homework 7.29 correlation function directory and file attributes related functions

基于MySQL数据库,Redis缓存,MQ消息中间件,ES搜索引擎的高可用方案解析

mapbox-gl开发教程(十四):画圆技巧

Breaking the principle and introducing SQL, what does MongoDB want to do???

程序环境和预处理(详解)

云原生应用的概念和云原生应用的 15 个特征

Apifox generates interface documentation tutorial and operation steps

Manage reading notes upward
随机推荐
Win11打不开exe应用程序怎么办?Win11无法打开exe程序解决方法
干货分享:小技巧大用处之Bean管理类工厂多种实现方式
Scheduling of combined electric-heating system based on multi-objective two-stage stochastic programming method
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
SCM engineers written questions induction summary
柔性机械系统分布参数建模及其控制的研究与进展
使用百度EasyDL实现明厨亮灶厨师帽识别
PanGu-Coder: Function-level code generation model
contentDocument contentWindow,canvas 、svg,iframe
概率论的学习整理2:如何对随机实验的对象:“事件” 进行计数呢? 四种计数方法,不只是排列组合
重写并自定义依赖的原生的Bean方法
[BJDCTF2020]Cookie is so stable-1|SSTI injection
为什么说Prometheus是足以取代Zabbix的监控神器?
[SCTF2019]Flag Shop
saltstack学习3模块
C# 时间戳与时间的互相转换
11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?
Verilog语法基础HDL Bits训练 07
Testability of Fuzzy Discrete Event Systems
概率论的学习和整理7:理解期望和方差还是要回到随机试验本身,期望不是平均值,方差的公式不同情况不同