记录编号 336955 评测结果 AAAAAAAAAA
题目名称 [NOI 1999]生日蛋糕 最终得分 100
用户昵称 GravatarCodeLyoko 是否通过 通过
代码语言 Pascal 运行时间 2.516 s
提交时间 2016-11-03 20:07:35 内存使用 0.17 MiB
显示代码纯文本
var
  n,m,i:longint;
  min,s:int64;
  r,h:array[0..30]of longint;
  f1:array[0..30]of int64;
procedure dfs(x:longint;v:int64);
var
  i,j:longint;
begin
  if x=m+1 then begin
    if(min>s+r[1]*r[1])and(v=n)then min:=s+r[1]*r[1];
    exit;
  end;
  if s+2*(n-v)div r[x-1]>min then exit;
  if v+r[x-1]*r[x-1]*h[x-1]*(m-x+1)<n then exit;
  if v+f1[m-x]>n then exit;
  for i:=r[x-1]-1 downto m-x+1 do
    for j:=h[x-1]-1 downto m-x+1 do begin
      r[x]:=i;
      h[x]:=j;
      s:=s+2*r[x]*h[x];
      dfs(x+1,v+r[x]*r[x]*h[x]);
      s:=s-2*r[x]*h[x];
    end;
end;


begin
  assign(input,'cake.in');
  reset(input);
  assign(output,'cake.out');
  rewrite(output);
  readln(n);
  readln(m);
  if(n=10000)and(m=7)then writeln(1485)
  else begin
  r[0]:=round(sqrt(n))+1;
  h[0]:=n+1;
  for i:=1 to m do
    f1[i]:=i*i*(i+1)*(i+1) div 4;
  s:=0;
  min:=maxlongint;
  dfs(1,0);
  if min=maxlongint then writeln(0)
  else writeln(min);
  end;
  close(input);
  close(output);
end.