记录编号 1823 评测结果 AAAAA
题目名称 [NOI 1999]生日蛋糕 最终得分 50
用户昵称 Gravatarname:弓虽 是否通过 通过
代码语言 Pascal 运行时间 10.000 s
提交时间 2008-09-08 20:42:41 内存使用 0.00 MiB
显示代码纯文本
program cgb; 
var 
n,m,r,h,i:longint; 
opt:longint; 
minv:array[0..20]of longint; 
function min(a,b:longint):longint; 
begin 
if a<b then min:=a else min:=b; 
end; 
function max(a,b:longint):longint; 
begin 
if a>b then max:=a else max:=b; 
end; 
function maxv(r,h,k:longint):longint; 
var i:longint; 
begin 
maxv:=0; 
for i:=1 to k do 
begin 
dec(r);dec(h); 
if (r<1)or(h<1) then exit; 
inc(maxv,r*r*h); 
end; 
end; 
procedure dfs(k,lr,lh,s,n:longint); 
var r,h,e,v,s1:longint; 
begin 
if k=0 then 
begin 
if n=0 then opt:=s; 
exit; 
end; 
for r:=lr-1 downto k do 
begin 
e:=min(n div (r*r),lh-1); 
for h:=e downto k do 
begin 
v:=n-r*r*h; 
s1:=s+2*r*h; 
if (v>0)or((k=1)and(v=0))then 
if (maxv(r,h,k-1)>=v)and(minv[k-1]<=v)and(s1<opt) then 
dfs(k-1,r,h,s1,v); 
end; 
end; 
end; 
begin 
assign(input,'cake.in');
assign(output,'cake.out');
reset(input);
rewrite(output);
readln(n,m); 
repeat 
opt:=maxlongint; 

r:=0;h:=0;minv[0]:=0; 
for i:=1 to m do 
begin 
inc(r);inc(h); 
minv[i]:=minv[i-1]+r*r*h; 
end; 
for r:=trunc(sqrt(n div m)) downto m do 
for h:=n div (r*r) downto m do 
if 2*r*h+r*r<opt then 
dfs(m-1,r,h,2*r*h+r*r,n-r*r*h); 
writeln(opt); 
until seekeof;
close(input);
close(output); 
end.