以洛谷P1036 选数为例
#include<bits/stdc++.h>
using namespace std;
int n, k, cnt, a[6000006];
bool think(int v) {//判断是否为质数
int flag = 0;
for (int i = 2; i * i <= v; i++)if (v % i == 0)flag = 1;
if (!flag&&v!=0&&v!=1)return true;
else return false;
}
int dfs(int pos, int ans, int c) {//深搜模块
if (pos == n + 1) {
if (c == k) {
if (think(ans)){
cnt++;
}
}
return 0;
}
for(int i=0;i<=1;i++){
c+=i;
ans+=a[pos]*i;
dfs(pos+1,ans,c);
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)cin >> a[i];
dfs(1,0,0);//初始化
cout<<cnt<<endl;
return 0;
}