Files
python/TangDou/LuoGuBook/P1164_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

27 lines
701 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 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;
}