记录编号 687 评测结果 ATAAAEEEEE
题目名称 [NOI 1999]生日蛋糕 最终得分 40
用户昵称 Gravatarrottenwood 是否通过 未通过
代码语言 Pascal 运行时间 2.618 s
提交时间 2008-07-21 08:48:18 内存使用 0.00 MiB
显示代码纯文本
program cake;
  var
    n,m,i,j:integer;
    ans,k:longint;
    f1,f2:text;
    a,b:array[1..14]of integer;
    procedure ini;
      begin
        assign(f1,'cake.in');reset(f1);
        assign(f2,'cake.out');rewrite(f2);
        readln(f1,n);
        readln(f1,m);
      end;

    procedure try(i,ri,hi,vi,si:integer);
      var
        k,j:integer;
        temp,temp1:integer;
      begin
        for k:=m-i to ri-1 do
          for j:=m-i to hi-1 do
           begin
            if k*k*j<=vi then
             begin
              temp:=vi-k*k*j;
              temp1:=si+2*k*j;
              if (i=m)and(temp=0)then begin
               if temp1<ans then ans:=temp1;
              end
              else begin
                if (i<m)and(temp>=a[i+1])and(si+2*vi/k<ans)and(temp<=b[i+1])
                 then try(i+1,k,j,temp,temp1);
              end;
             end;
           end;
      end;
    procedure main;
      var
        i,j,k:integer;
      begin
        ans:=200000000;
        for i:=m to trunc(sqrt(n))+1do
          for j:=m to n div(i*i)do begin
            b[1]:=0;
            for k:=2 to m do
              b[k]:=b[k-1]+(i-1)*(i-1)*(j-1);
            try(2,i,j,n-i*i*j,i*i+2*i*j);
          end;
        if ans<200000000 then write(f2,ans)
          else write(f2,'0');
      end;
    procedure min;
      var
        i,j:integer;
      begin
        a[m]:=1;
        for i:=m-1 downto 1 do begin
          j:=m-i+1;
          a[i]:=a[i+1]+j*j*j;
        end;
      end;
  begin
    ini;
    if m=1 then begin
     k:=20000000;
     for i:=1 to trunc(sqrt(n))do begin
       j:=n div(i*i);
       if j*i*i=n then begin
       if 2*i*j+i*i<k then k:=2*i*j+i*i;
       end;
     end;
     write(f2,k);
    end
    else begin
      min;
      main;
    end;
    close(f1);
    close(f2);
  end.