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.

35 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int a[N];
//是不是质数
bool isPrime(int n) {
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
int main() {
int n, k, ans = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int U = 1 << n; //U-1即为全集 ,比如 1<<5 就是 2的5次方就是32U=32。而U-1=31就是表示 1 1 1 1 1
for (int S = 0; S < U; S++) { //枚举所有子集[0,U)
//找到k元子集
if (__builtin_popcount(S) == k) {
int sum = 0;
//是哪些数存在于子集中呢?
for (int i = 0; i < n; i++) {
int bit = (S >> i) & 1;
if (bit) sum += a[i]; //遍历数字S的每一位如果不是0表示这一位上的数字是存在的需要加进来
}
//判断子集元素和是不是素数
if (isPrime(sum)) ans++;
}
}
cout << ans << endl;
return 0;
}