比赛 暑假培训三 评测结果 AAATTT
题目名称 数列问题 最终得分 50
用户昵称 苏轼 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-07-19 10:15:57
显示代码纯文本
program dfs3;

var n,i:integer;
    ans:longint;
    s:array[1..50]of integer;
    f:array[1..50]of boolean;
    txt:text;

procedure output;
 var i:integer;
 begin
  for i:=1 to n do
   write(txt,s[i],' ');
  writeln(txt);
 end;

function prime:boolean;
 var tmp,i,j:integer;
 begin
  prime:=true;

  for i:=1 to n-1 do
  begin
   tmp:=s[i]+s[i+1];

   if not(odd(tmp)) then
   begin
    prime:=false;
    exit;
   end;

   for j:=3 to trunc(sqrt(tmp)) do
    if odd(j) then
     if tmp mod j=0 then
     begin
      prime:=false;
      exit;
     end;
  end;
 end;

procedure search(a:integer);
 var p:boolean;
     i:integer;
 begin
  if a<=n then
   for i:=1 to n do
   begin
    if not(f[i]) then
    begin
     s[a]:=i;
     f[i]:=true;
     search(a+1);
     f[i]:=false;
    end;
   end else
    if prime then
    begin
     output;
     inc(ans);
    end;
 end;

begin
 assign(txt,'dfs3.in');
 reset(txt);
 readln(txt,n);
 close(txt);

 assign(txt,'dfs3.out');
 rewrite(txt);
 search(1);
 writeln(txt,ans);
 close(txt);
end.