求助

B3624 猫粮规划

CSP_JAKME @ 2024-08-25 16:25:06

#include<bits/stdc++.h>
using namespace std;
int n,l,r,a[41],cnt=0;
void dfs(int step,int sum){
    if(sum>=l&&sum<=r){
        cnt++;
        return;
    }
    if(step==n+1) return;
    dfs(step+1,sum);
    dfs(step+1,sum+a[step]);
    sum-=a[step];
    return;
}
int main(){
    cin >> n >> l >> r;
    for(int i=1;i<=n;i++) cin >> a[i];
    dfs(1,0);
    cout << cnt;
    return 0;
}

by goIdie @ 2024-08-25 16:29:44

@CSP_JAKME ,求关

#include<bits/stdc++.h>
using namespace std;
long long n,l,r,w[10001],ans;
void dfs(int x,int y){
    if(y>r){
        return;
    }
    if(x>n){
        if(y>=l&&y<=r){
            ans++;
        }
        return;
    }
    dfs(x+1,y+w[x]);
    dfs(x+1,y);
    return;
}
int main(){
    cin>>n>>l>>r;
    for(int i=1;i<=n;i++){
        cin>>w[i];
    }
    dfs(1,0);
    cout<<ans;
} 

by CSP_JAKME @ 2024-08-25 16:30:46

@goIdie e,刚AC


|