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.

73 lines
1.9 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
/*
register intregister 使cpu
int 1e8使int
*/
/**
*
*
* 2019-11-27
* @param x
* @return
* inline http://c.biancheng.net/view/199.html
*/
inline bool prime(int x) {
for (register int i = 2; i <= sqrt(x); i++)
if (x % i == 0)
return false;
return true;
}
int a[10001]; //可以用一个a数组来记录我们所选的数
int n; // n是所有整数的个数
int k; // k是选择整数的个数
int total; //total为总方案数
/**
*
*
* 2019-11-27
* @param step :
* @param sum :
* @param cnt :
*/
inline void dfs(int step, int sum, int cnt) {
//如果已经进行到了n+1次或者是已经选了k个数
if (step == n + 1 || cnt == k) {
//如果sum为一个素数且已经选了k个数
if (prime(sum) && cnt == k) {
total++; //总方案数+1
}
return;
}
//继续枚举不选择下一个数的情况
dfs(step + 1, sum, cnt);
//继续搜索,选择下一个数
dfs(step + 1, sum + a[step], cnt + 1);
return;
}
int main() {
//读入输出优化的强迫症
ios::sync_with_stdio(false);
//输入n和k
cin >> n >> k;
//输入数据
for (register int i = 1; i <= n; i++) {
cin >> a[i];
}
//从第1个数开始搜
dfs(1, 0, 0);
//输出结果
cout << total;
return 0;
}