peapapig @ 2024-07-09 12:10:52
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+9;
long long n,m,a[N],b[N],cnta,cntb,numa[N],numb[N],suma,sumb,ans;
void dfs(long long x,long long y){
//cout<<x<<" "<<y<<endl;
if(y>m)return;
if(x==cnta+1){
numa[++suma]=y;
return;
}
//cout<<a[x]<<endl;
dfs(x+1,y+a[x]);
dfs(x+1,y);
}
void dfs2(long long x,long long y){
if(y>m)return;
if(x==cntb+1){
//cout<<x<<" "<<y<<endl;
numb[++sumb]=y;
return;
}
dfs2(x+1,y+b[x]);
dfs2(x+1,y);
}
int main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
long long x;
scanf("%lld",&x);
if(i<=n/2)a[++cnta]=x;
else b[++cntb]=x;
}
// for(int i=1;i<=cnta;i++)cout<<a[i]<<" ";
// cout<<endl;
dfs(1,0);
dfs2(1,0);
sort(numa+1,numa+cnta+1);
//for(int i=1;i<=suma;i++)cout<<a[i]<<" ";
//cout<<endl;
for(int i=1;i<=sumb;i++)
ans+=upper_bound(numa+1,numa+suma+1,m-numb[i])-numa-1;
printf("%lld",ans);
return 0;
}
by lihyit @ 2025-01-10 16:23:40
这条可以使你拿到40pts:
#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;
}
DFS枚举,60分都是TLE pwp