记录编号 13781 评测结果 AAAAAA
题目名称 [HAOI 2004模拟]数列问题 最终得分 100
用户昵称 Gravatarmaxiem 是否通过 通过
代码语言 Pascal 运行时间 1.038 s
提交时间 2009-10-10 10:57:15 内存使用 0.12 MiB
显示代码纯文本
program dfs3;
var
  i,j,n:integer;
  l,p:array [1..50] of integer;
  num:array [1..50,1..50] of integer;
  flag:array [1..50] of boolean;
  s:array [1..99] of boolean;
  sum:longint;
procedure search(step:integer);
var i,t,k:integer;
begin
  if step=n+1 then begin
    write (p[1]);
    for i:=2 to n do write (' ',p[i]);
    sum:=sum+1;
    writeln;
  end
  else begin
    k:=p[step-1];
    for i:=1 to l[k] do begin
      t:=num[k,i];
      if flag[t]=false then begin
        p[step]:=t;
        flag[t]:=true;
        search(step+1);
        flag[t]:=false;
      end;
    end;
  end;
end;
begin
  fillchar (s,sizeof(s),0);
  fillchar (l,sizeof(l),0);
  fillchar (num,sizeof(num),0);
  for i:=1 to 99 do begin
    for j:=2 to trunc(sqrt(i)) do if i mod j=0 then begin
      s[i]:=true;
      break;
    end;
  end;
  assign (input,'dfs3.in');
  reset (input);
  readln (n);
  for i:=2 to 2*n-1 do for j:=1 to n do if (i-j>0)and(s[i]=false)and(i-j<=n) then begin
    inc(l[j]);
    num[j,l[j]]:=i-j;
  end;
  close (input);
  assign (output,'dfs3.out');
  rewrite (output);
  for i:=1 to n do begin
    fillchar (flag,sizeof(flag),0);
    p[1]:=i;flag[i]:=true;
    search (2);
  end;
  writeln (sum);
  close (output);
end.