【题解】P1036 选数

题面

题目传送门

前言

卡了一小会,算了复杂度不用欧拉筛预处理

正文

思路是很简单的,比较考验像云落一般没有超级玛丽的选手

考虑暴搜每一种可能的方案,依次判断其是否为质数即可

直接写出一个 DFS 函数

考虑到 \(n \le 20\) 并且这是一道远古老题,故钦定其时间复杂度过关

代码

#include<iostream>
#define int long long
using namespace std;
const int maxn=25;
int n,k,a[maxn],ans;
inline bool check(int x){
    for(int i=2;i*i<=x;i++){
        if(x%i==0){
            return false;
        }
    }
    return true;
}
inline void dfs(int cnt,int sum,int st){
    if(cnt==k){
        if(check(sum)){
            ans++;
        }
        return;
    }
    for(int i=st;i<=n;i++){
        dfs(cnt+1,sum+a[i],i+1);
    }
    return;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    dfs(0,0,1);
    cout<<ans<<endl;
    return 0;
}

后记

云落好菜

完结撒花!

posted @ 2025-02-05 20:31  sunxuhetai  阅读(16)  评论(0)    收藏  举报