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.

50 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
/*
http://oj.521ma.cn/problem.php?id=1302
https://www.bilibili.com/video/av88194897/
i:i
j:j
dp f(i,j) --->---->im
--->----> count
dp i ----> -->f(i-1,j)
----> -->f(i-1,j-a[i])
f(i,j)=f(i-1,j)+f(i-1,j-ai) ---->f(j)=f(j)+f(j-ai)
*/
const int N = 1001;
int a[N], f[N];
int n, m;
int main() {
//输入+输出重定向
freopen("../1302.txt", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
//初始化 因为如果和为0那也就是什么都不放那就是1种方案所以初始化f[0]=1,
f[0] = 1;
//遍历每一个数字
for (int i = 1; i <= n; i++)
for (int j = m; j >= a[i]; j--) //和01背包一样需要倒序
f[j] = f[j] + f[j - a[i]]; //求count问题
cout << f[m] << endl; //输出结果
//关闭文件
fclose(stdin);
return 0;
}