You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}