题解:P1036 [NOIP 2002 普及组] 选数
思路
考虑 dfs,
代码
#include<iostream>
#include <cmath>
#include <cstdio>
#define MAXN 25
using namespace std;
int n, k, num[MAXN], visit[1001];
int cnt, ans;
bool ss(int n) {
if(n == 1) return 0;
if(n == 2) return 1;
for(int i = 2; i <= sqrt(n); i++)
if(n % i == 0) return 0;
return 1;
}
void DFS(int start, int end) {
if(start > k) {
if(ss(cnt)) ans++;
return ;
}
else {
for(int i = end + 1; i <= n; i++) {
cnt += num[i];
DFS(start + 1, i);
cnt -= num[i];
}
}
}
int main() {
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> num[i];
DFS(1,0);
cout << ans << endl;
return 0;
}