vijos-p1128 2008.11.6

vijos-p1128 2008.11.6

描述 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 Format 

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

x1,x2,…,xn (1<=xi<=5000000 

输出格式 Output Format 

一个整数(满足条件的种数)。 

样例输入 Sample Input  

43

37 12 19

样例输出 Sample Output  

program p_1128;
var a:array[1..20]of longint;
      n,k,sum,x,i,j:longint;
procedure init;
begin
  read(n,k);
  for i:=1 to n do read(a[i]);
end;
function choose(x:longint):boolean;
var  t:longint;
begin
  choose:=true;
  for t:=2 to trunc(sqrt(x)) do
      if x mod t=0 then exit(false);
end;
procedure find(x:longint);
begin
  if choose(x) then inc(sum);
end;
procedure deal(r,x,m:longint);
var j:longint;
begin
   if r=k+1 then begin find(x);exit end;
    for j:=m+1 to n-k+r do
       begin  inc(x,a[j]);deal(r+1,x,j);dec(x,a[j]);end;
end;
procedure doit;
begin
       sum:=0;
       for i:=1 to n-k+1 do
          begin
             x:=a[i];deal(2,x,i);
           end;
end;
{--------------------}
begin
  init;
  doit;
  writeln(sum);
 end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值