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

30 lines
861 B
C++
Raw Permalink 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];
int main() {
//文件输入
freopen("QiaHao_01.in", "r", stdin);
scanf("%d %d", &n, &m);
// 恰好,在有前i种物品可以选择的情况下,只有空间为0有一种方案,就是啥都不选
for (int i = 0; i <= n; i++) f[i][0] = 1;
// 由于下面的代码中,每一行都可以从上一行迁移而来,事实上不用真的循环把第一列全部设置为1,
// 只需把f[0][0]=1,即可完成递推关系建立。
// f[0][0] = 1;
for (int i = 1; i <= n; i++) {
int v;
scanf("%d", &v);
for (int j = 0; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v) f[i][j] += f[i - 1][j - v];
}
}
printf("%d\n", f[n][m]);
return 0;
}