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

35 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

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]; //第一维:前i个物品,第二维:体积j及以下
int main() {
//文件输入
freopen("ZhiDuo_01.in", "r", stdin);
scanf("%d %d", &n, &m);
//初始化
//在前0个物品中选,所有合法空间,都有一种结果,就是啥也不要,啥也不要也满足“不超过j”这个条件,方案数=1。
for (int i = 0; i <= m; i++) f[0][i] = 1;
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 - 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]; //不被i件物品
// if (j >= v) f[i][j] += f[i - 1][j - v]; //能装下就要加上可以转移过来状态的方案数
// }
}
printf("%d\n", f[n][m]);
return 0;
}