You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int a[N];
int n, K;
int cnt;
//判断一个数是不是质数
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
/**
* :
* @param step
* @param sum
* @param m
* @return
*/
void dfs(int step, int sum, int m) {
//脚落地
if (m == k) {
if (isPrime(sum)) cnt++;
return;//已经完成选择k个数判断和是不是质数.如果是质数,说明找到了一种合法解
}
//这句需在放在后面
if (step == n + 1) return;
//选择当前数字
dfs(step + 1, sum + a[step], m + 1);
//放弃当前数字
dfs(step + 1, sum, m);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1, 0, 0);
cout << cnt << endl;
return 0;
}