Description
已知 �n 个整数 �1,�2,⋯ ,��x1,x2,⋯,xn,以及 11 个整数 �k(�<�k<n)。从 �n 个整数中任选 �k 个整数相加,可分别得到一系列的和。例如当 �=4n=4,�=3k=3,44 个整数分别为 3,7,12,193,7,12,19 时,可得全部的组合与它们的和为:
3+7+12=223+7+12=22
3+7+19=293+7+19=29
7+12+19=387+12+19=38
3+12+19=343+12+19=34
现在,要求你计算出和为素数共有多少种。
例如上例,只有一种的和为素数:3+7+19=293+7+19=29。
Input
第一行两个空格隔开的整数 �,�n,k(1≤�≤201≤n≤20,�<�k<n)。
第二行 �n 个整数,分别为 �1,�2,⋯ ,��x1,x2,⋯,xn(1≤��≤5×1061≤xi≤5×106)。
Output
输出一个整数,表示种类数。
Sample 1
Inputcopy | Outputcopy |
---|---|
4 3 3 7 12 19 | 1 |
Hint
【题目来源】
NOIP 2002 普及组第二题
#include<bits/stdc++.h>
typedef long long ll;
#define endl '\n'
#define buff ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
using namespace std;
int n,k;
int a[29];
int ans=0;
bool su(int x)
{
for(int i=2;i*i<=x;i++)
{
if(x%i==0) return 0;
}
return 1;
}
void dfs(int m,int sum,int xx)
{
if(m==k+1)
{
if(su(sum)) ans++;
}
for(int i=xx;i<=n;i++)
{
dfs(m+1,sum+a[i],i+1);
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
dfs(1,0,1);
cout<<ans<<'\n';
}