求助

B3624 猫粮规划

szlh_XJS @ 2024-07-08 16:00:07

#include <iostream>
using namespace std;

int n, l, r, s;
int vis[45], w[45];

void dd(int t) {
    if (t >= l && t <= r) {
        s++;
    }
    if (t > r) {
        return;
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            vis[i] = 1;
            dd(t + w[i]);
            vis[i] = 0;
        }
    }
}

int main() {
    cin >> n >> l >> r;
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
    }
    dd(0);
    cout << s;
    return 0;
}

by Qinglan2011 @ 2024-07-08 16:32:17

#include <iostream>
using namespace std;
int n,l,r,ans,a[1000000],w[1000000]; 
void dfs(int t){
    int sum=0;
    for (int i=1;i<=t-1;i++) sum+=w[i]*a[i];
    if (sum>r) return;
    if (t==n+1){
        if (l<=sum&&sum<=r) ans++;
        return;
    }
    for (int i=0;i<=1;i++){
        a[t]=i;
        dfs(t+1);
    }
    return;
}
int main(){
    scanf("%d%d%d",&n,&l,&r);
    for (int i=1;i<=n;i++) scanf("%d",&w[i]);
    dfs(1);
    printf("%d",ans);
    return 0;
}

by Qinglan2011 @ 2024-07-08 16:34:24

@szlh_XJS应该差不多,你的深搜前面是i=1,但是后面是dd(0)


by Qinglan2011 @ 2024-07-08 16:36:00

@szlh_XJS 可以自己对照着看看


by Qinglan2011 @ 2024-07-08 16:37:07

(求关)


|