Files
python/GESP/Mkx/20250401T4.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
1.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N]; // n个数字都是什么数字
int n, k; // 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;
}
/*
测试用例
4 3
3 7 12 19
答案:
1
*/
void dfs(int u, int c, int sum) {
if (u == n + 1) { // 递归出口
if (c == k) { // 只有最终是3个数字的组合才有意义
if (isPrime(sum))
cnt++; // 多发现了一个质数
}
return;
}
if (c < k) { // 数量不足,才可以继续选择数字,否则就没有机会再选择数字了
// 不要这个数字
dfs(u + 1, c, sum);
// 要这个数字
dfs(u + 1, c + 1, sum + a[u]);
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
dfs(1, 0, 0);
cout << cnt << endl;
return 0;
}