求助求助求助!!!,调了一天了,不知道哪里错了,只有16分
查看原帖
求助求助求助!!!,调了一天了,不知道哪里错了,只有16分
602655
adomais楼主2022/3/16 12:02
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 25;
int n, k;
int s;
int ans;
int a[N];
bool used[N];

bool check(int x){
    if(x == 0 || x == 1) return false;
    for(int i = 2; i * i <= x; i++)
        if(x % i == 0) return false;
    return true;
}

void dfs(int u, int start){
    if(u > k){
        if(check(s)) ans++;
        return;
    } 
    
    for(int i = start; i <= n; i++){
        if(!used[i]){
            used[i] = true;
            s += a[i];
            dfs(u + 1, start + 1);
            s -= a[i];
            used[i] = false;
        }
    }
}

int main(){
    cin >> n >> k;
    
    for(int i = 1; i <= n; i++)
        cin >> a[i];
        
    dfs(1, 1);
    
    cout << ans << endl;
    
    return 0;
}
2022/3/16 12:02
加载中...