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.

27 lines
701 B

This file contains ambiguous Unicode 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 a[N];
int n, m;
//纯递归思路最后一个测试点TLE
//x:第几个位置
//m:钱数上限
int dfs(int x, int m) {
int ans = 0;
if (m == 0) return 1;//钱花光,增加一种办法
if (x == n) return 0;//选择完n种钱还没有花光无效方法
//看下一个菜,如果钱还够
if (a[x + 1] <= m)
ans += dfs(x + 1, m - a[x + 1]); //选择当前位置的菜
ans += dfs(x + 1, m); //不选择当前位置的菜
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> a[i];
cout << dfs(0, m) << endl;
return 0;
}