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
1
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.