TLE求助

B3624 猫粮规划

LichKing_Arthas @ 2024-08-11 21:02:34

#include<bits/stdc++.h>
using namespace std;
int n,l,r,s,ans,food[50],a[50];
void dfs(int k,int ii,int num){
    if(k>num){
        if(ans>=l && ans<=r)  s++;
    }else{
        for(int i=ii;i<=n;i++)
            if(a[i]==0){
                a[i]=1;
                ans+=food[i];
                dfs(k+1,i+1,num);
                a[i]=0,ans-=food[i];
            }
    }
}int main(){
    scanf("%d%d%d",&n,&l,&r);
    for(int i=1;i<=n;i++)  cin>>food[i];
    for(int num=1;num<=n;num++)  dfs(1,1,num);
    printf("%d",s);
    return 0;
}

by AquaDaMean1e @ 2024-08-11 22:07:24

#include<bits/stdc++.h>
using namespace std;
int n,l,r,s,ans,food[50],a[50];
void dfs(int k,int ii,int num){
    if(ans>r){
        return;
    }
    if(ans>=l && ans<=r){
        s++;
    }
    for(int i=ii;i<=n;i++) {
        if(a[i]==0){
            a[i]=1;
            ans+=food[i];
            dfs(k+1,i+1,num);
            a[i]=0,ans-=food[i];
        }
    }
}int main(){
    scanf("%d%d%d",&n,&l,&r);
    for(int i=1;i<=n;i++)  cin>>food[i];
    dfs(1,1,1);
    printf("%d",s);
    return 0;
}

by AquaDaMean1e @ 2024-08-11 22:07:33

@LichKing_Arthas


by LichKing_Arthas @ 2024-08-12 16:22:40

好吧,我写错了,%拜大佬


|