主题:排列组合
已知n(1<=n<=20)个整数x1,x2,…,xn(1<=xi<=5000000),以及一个整数k(k<n)。从n个整数中任选k个整数相加,可分别得到一系列的和。现在,要求你计算出和为素数共有多少种。
const max=20;
var
a:array [1..max] of longint;
use:array [1..max] of boolean;
n,i,k,tot:integer;
procedure panduan(s:longint);
var i,x,m:longint;
y:real;
begin
y:=sqrt(s);
x:=round(y)+1;
repeat
m:=s mod i;
inc(i);
until (i=x)or(m=0);
if m=0 then tot:=tot+1;
end;
procedure dfs(now:integer;ans:integer;sum:longint);
var i:integer;
s:longint;
begin
for i:=now to n do
if use[i] then
begin
use[i]:=false;
if ans=k then
begin
panduan(s);
dfs(now+1,0,0);
end
else
begin
s:=sum+a[i];
dfs(now+1,ans+1,s);
end;
use[i]:=true;
end;
end;
begin
readln(n,k);
for i:=1 to n do read(a[i]);
fillchar(use,sizeof(use),true);
tot:=0;
dfs(1,0,0);
writeln(tot);
end.
帮我看看哪里错了…
谢谢。
const max=20;
var
a:array [1..max] of longint;
use:array [1..max] of boolean;
n,i,k,tot:integer;
procedure panduan(s:longint);
var i,x,m:longint;
y:real;
begin
y:=sqrt(s);
x:=round(y)+1;
repeat
m:=s mod i;
inc(i);
until (i=x)or(m=0);
if m=0 then tot:=tot+1;
end;
procedure dfs(now:integer;ans:integer;sum:longint);
var i:integer;
s:longint;
begin
for i:=now to n do
if use[i] then
begin
use[i]:=false;
if ans=k then
begin
panduan(s);
dfs(now+1,0,0);
end
else
begin
s:=sum+a[i];
dfs(now+1,ans+1,s);
end;
use[i]:=true;
end;
end;
begin
readln(n,k);
for i:=1 to n do read(a[i]);
fillchar(use,sizeof(use),true);
tot:=0;
dfs(1,0,0);
writeln(tot);
end.
帮我看看哪里错了…
谢谢。