lihyit @ 2025-01-10 16:14:55
#include<bits/stdc++.h>
using namespace std;
int a[50],ans;
int n,m;
void dfs(int num,int sum)
{
if(num>n)
{
if(sum<=m)ans++;
return ;
}
dfs(num+1,sum+a[num]);
dfs(num+1,sum);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
dfs(1,0);
cout<<ans<<endl;
return 0;
}
by lihyit @ 2025-01-10 16:18:44
这个算法,50不行,顶多40 40解:
#include<bits/stdc++.h>
using namespace std;
long long a[50],ans;
long long n,m;
void dfs(long long num,long long sum)
{
if(num>n)
{
if(sum<=m)ans++;
return ;
}
dfs(num+1,sum+a[num]);
dfs(num+1,sum);
}
int main()
{
cin>>n>>m;
for(long long i=1;i<=n;i++) cin>>a[i];
dfs(1,0);
cout<<ans<<endl;
return 0;
}