记录编号 1697 评测结果 AAAAAA
题目名称 [HAOI 2004模拟]数列问题 最终得分 100
用户昵称 Gravatarzhai 是否通过 通过
代码语言 Pascal 运行时间 1.052 s
提交时间 2008-09-06 21:28:10 内存使用 0.12 MiB
显示代码纯文本
program dfs3;
  var
    a:array[1..50]of boolean;
    b:array[1..50]of integer;
    s:array[0..25]of integer;
    h:array[1..50,0..50]of integer;
    n,g:integer;
    e:longint;
    f1,f2:text;
    procedure init;
      var
        i:integer;
      begin
        assign(f1,'dfs3.in');reset(f1);
        assign(f2,'dfs3.out');rewrite(f2);
        read(f1,n);
        close(f1);
        for i:=1 to n do a[i]:=true;
      end;
    procedure jian;
      var
        i,j,k:integer;
        f:boolean;
      begin
        s[1]:=2;
        k:=2;
        for i:=3 to n+n-1 do begin
          f:=true;
          j:=2;
          while f and (j<trunc(sqrt(i))+1) do
            if i mod j =0 then f:=false
            else inc(j);
          if f then begin
            s[k]:=i;
            inc(k);
          end;
          s[0]:=k-1;
        end;
      end;
    procedure li;
      var
        i,j:integer;
      begin
        for i:=1 to n do
          for j:=2 to s[0] do if (s[j]-i<>i)and(s[j]>i)and(s[j]-i<=n) then
            begin
              inc(h[i,0]);
              h[i,h[i,0]]:=s[j]-i;
            end;
      end;
    procedure chuli;
      var
        k:integer;
      begin
        inc(e);
        for k:=1 to n do write(f2,b[k],' ');
        writeln(f2);
      end;
    procedure try(x,i:integer);
      var
        j,k:integer;
      begin
        for j:=1 to h[x,0] do begin
          if a[h[x,j]] then begin
            b[i]:=h[x,j];
            a[b[i]]:=false;
            if i=n then chuli
            else try(b[i],i+1);
            a[b[i]]:=true;
          end;
        end;
      end;
  begin
    init;
    e:=0;
    fillchar(b,sizeof(b),0);
    fillchar(s,sizeof(s),0);
    jian;
    li;
    for g:=1 to n do begin
      b[1]:=g;
      a[g]:=false;
      try(g,2);
      a[g]:=true;
    end;
    write(f2,e);
    close(f2);
  end.