Description Title
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).
Input and output format
input format:
Keyboard input format:
n , k (1<=n<=20,k<n)
x1,x2,…,xn (1<=xi<=5000000)
Output formats:
Screen output format:
An integer (satisfy several criteria).
Input Output Sample
Input Sample # 1:
4 3
3 7 12 19
Output Sample # 1:
1
----------------
idea: DFS search then determines whether or not a prime number and satisfy several conditions is +1
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int a[10001];
int n,k,sum,tot;
inline bool prime(int x)
{
for(register int i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
inline void dfs(int step,int s,int count)
{
if(step==n+1 || count == k)
{
if(prime(s)&& count == k)
tot++;
return;
}
dfs(step+1,s+a[step],count+1);
dfs(step+1,s,count);
return;
}
inline void print(){
printf("%d",tot);
}
int main(){
scanf("%d %d",&n,&k);
for(register int i=1;i<=n;i++)
scanf("%d",&a[i]);
dfs(1,0,0);
print();
return 0;
}