素数环

来源:互联网 发布:木工雕刻机编程软件 编辑:程序博客网 时间:2024/05/29 10:10

这道题大胆去搜,不用怕超时。

打印时很吓人。呵呵呵!


var a:array[0..21] of longint;

    b:array[0..21] of boolean;
    s:longint;
function bool(x,y:longint):boolean;
var i:longint;
begin
  for i:=2 to trunc(sqrt(x+y)) do
    if (x+y) mod i=0 then exit(false);
  exit(true);
end;
procedure print;
var i:longint;
begin
  inc(s);
  write(s,':');
  for i:=1 to 20 do write(a[i],' ');
  writeln;
end;
procedure try(t:longint);
var i:longint;
begin
  if t>20 then
  begin
    if bool(a[20],a[1]) then print;
    exit;
  end;
  for i:=1 to 20 do
    if bool(a[t-1],i) and(b[i]) then
    begin
      a[t]:=i;
      b[i]:=false;
      try(t+1);
      b[i]:=true;
    end;
end;
begin
  fillchar(b,sizeof(b),true);
  try(1);
  writeln('ans=',s);

end.

1 0
原创粉丝点击