Files
python/TangDou/AcWing/BeiBao/FangAnShu/ZhiDuo_WQ_ErWei.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

39 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
int f[N][N];
int main() {
//文件输入
freopen("ZhiDuo_WQ.in", "r", stdin);
scanf("%d %d", &n, &m);
for (int i = 0; i <= m; i++) f[0][i] = 1; //前0种物品中选择,最多不超过m的体积下,选择的方案只有一种合法,就是啥也不选
for (int i = 1; i <= n; i++) {
int v;
scanf("%d", &v);
/*写法1*/
for (int j = 0; j <= m; j++) {
if (j >= v)
f[i][j] = f[i - 1][j] + f[i][j - v];
//注意这里与01背包的差别!!!
// 在01背包的状态转移中,i就是i,它之前不能出现过i,只能从i-1转移
// 在完全背包的状态转移中,i出现过没关系,只要能装下,出现几个都行,可以从i转移
// 01背包:
// f[i][j] = f[i - 1][j] + f[i - 1][j - v];
else
f[i][j] = f[i - 1][j];
}
/*写法2*/
// for (int j = 0; j <= m; j++) {
// f[i][j] = f[i - 1][j];
// if (j >= v) f[i][j] += f[i][j - v];
// }
}
printf("%d\n", f[n][m]);
return 0;
}