有没有大佬帮忙看看,只过了#2和#5,感谢!!
查看原帖
有没有大佬帮忙看看,只过了#2和#5,感谢!!
392455
Giperx楼主2024/3/10 21:01
#include<iostream>
#include<set>
using namespace std;

set<int> st;

int a[25];
int n, k;

bool check(int d){
    if (d < 2) return false;
    else{
        for(int i = 2; i <= d / i; i++){
            if(d % i == 0) return false;
        }
    }
    return true;
}


void dfs(int deep, int tmp, int cnt){
    if ((n - cnt + 1) < (k - deep + 1)) return;

    if (deep > k){
//        cout << deep << ' ' << tmp << endl;
        if (check(tmp)) st.insert(tmp);
        return;
    }
    else{
        for(int i = cnt; i <= n; i++){
            tmp += a[i];
            dfs(deep + 1, tmp, i + 1);
            tmp -= a[i];
        }
    }
}

int main(){


    cin >> n >> k;

    for(int i = 1; i <= n; i++) cin >> a[i];

    dfs(1, 0, 1);

    cout << st.size();
    return 0;
}
2024/3/10 21:01
加载中...