Title Description
Known n integers x1, x2, ..., xn, and an integer k (k <n).
Optionally adding integers k from n integers in a range of available respectively.
For example, when n = 4, k = 3,4 are integers when 3,7,12,19, available with all combinations thereof and is:
3+7+12=22 3+7+19=29 7+12+19=38 3+12+19=34。
Now, we ask you to calculate the total number of species and is a prime number.
For example the embodiment, only one, and is a prime number: 3 + 7 + 19 = 29.
Entry
The first line of two integers: n, k (1 <= n <= 20, k <n)
The second line n integers: x1, x2, ..., xn (1 <= xi <= 5000000)
Export
An integer (number satisfying the condition of the program).
Sample input
4 3
3 7 12 19
Sample Output
1
Source Code
#include <iostream>
#include <cmath>
#include <string.h>
#include <stdio.h>
using namespace std;
int n,m,a[100],s[100],sum,ans;
bool isPrime(int);
void dfs(int x,int y)
{
if(x == m)//如果挑选出来3组数据,就求和,小于3个数,就继续挑选
{
sum = 0;
for(int i = 1;i <= x;i ++)
{
printf("a[%d] = %d\n",i,a[i]);
sum += s[a[i]];//对挑出来的三个数t求和
}
cout << "------------" << endl;
if(isPrime(sum) == 1)
ans ++;//如果是和是素数,就+1
return ;
}
y ++;
for(int i = y;i <= n;i ++)//从4个数中挑选三个数字
{
a[x + 1] = i;//改变下标形成新的挑选数
dfs(x + 1,i);//当x + 1等于3 就开始求和 如果小于3就继续挑选
}
}
bool isPrime(int r)//判断是否是素数
{
for(int i = 2;i <= sqrt(r);i ++)
if(r % i == 0)
return 0;
return 1;
}
int main()
{
cin >> n >> m;
for(int i = 1;i <= n;i ++)
cin >> s[i];
dfs(0,0);
cout << ans << endl;
return 0;
}