codevs——1008 选数

1008 选数

 

2002年NOIP全国联赛普及组

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述  Description

已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n)。从 n 个整数中任选 k 个整数相加,可分别得到一系列的和。例如当 n=4,k=3,4 个整数分别为 3,7,12,19 时,可得全部的组合与它们的和为:
    3+7+12=22  3+7+19=29  7+12+19=38  3+12+19=34。
  现在,要求你计算出和为素数共有多少种。
  例如上例,只有一种的和为素数:3+7+19=29)。

输入描述  Input Description

 键盘输入,格式为:
  n , k (1<=n<=20,k<n)
  x1,x2,…,xn (1<=xi<=5000000)

输出描述  Output Description

屏幕输出,格式为:
  一个整数(满足条件的种数)。

样例输入  Sample Input

4 3
3 7 12 19

样例输出  Sample Output

1

数据范围及提示  Data Size & Hint

(1<=n<=20,k<n)
(1<=xi<=5000000)

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 50
using namespace std;
int n,m,a[N],ans,sum,pos;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int pd(int x)
{
    for(int i=2;i*i<=x;i++)
     if(x%i==0) return false;
    return true;
}
void dfs(int k)
{
    if(pos==m) {ans+=pd(sum); return ;}
    for(int i=k;i<=n;i++)
    {
        pos++,sum+=a[i];
        dfs(i+1);
        sum-=a[i],pos--;
     } 
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++) a[i]=read();
    dfs(1);
    printf("%d",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/z360/p/7599297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值