vijos_1080
Function(Function(F…-20min-搜索
Hint
1、判定“-1 -1 -1”出错
2、该函数事实上满足w[a,a,a]:=2^a;当 (a<=b) or (a<=c) 时 w:=2^a
3、根本不用在前面加判断,直接把函数值赋给数组
?Download vijos_1080.pas
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | var a,b,c:longint; m:array[0..20,0..20,0..20] of longint; function w(a,b,c:longint):longint; var p,q,r,s:longint; begin if (a<=0)or(b<=0)or(c<=0) then w:=1 else if (a>20)or(b>20)or(c>20) then if m[20,20,20]<>0 then w:=m[20,20,20] else w:=w(20,20,20) else if (a<b)and(b<c) then begin if m[a,b,c-1]<>0 then p:=m[a,b,c-1] else p:=w(a,b,c-1); if m[a,b-1,c-1]<>0 then q:=m[a,b-1,c-1] else q:=w(a,b-1,c-1); if m[a,b-1,c]<>0 then r:=m[a,b-1,c] else r:=w(a,b-1,c); w:=p+q-r; m[a,b,c]:=p+q-r; end else begin if m[a-1,b,c]<>0 then p:=m[a-1,b,c] else p:=w(a-1,b,c); if m[a-1,b-1,c]<>0 then q:=m[a-1,b-1,c] else q:=w(a-1,b-1,c); if m[a-1,b,c-1]<>0 then r:=m[a-1,b,c-1] else r:=w(a-1,b,c-1); if m[a-1,b-1,c-1]<>0 then s:=m[a-1,b-1,c-1] else s:=w(a-1,b-1,c-1); w:=p+q+r-s; m[a,b,c]:=p+q+r-s; end; end; begin readln(a,b,c); while not((a=-1)and(b=-1)and(c=-1)) do begin writeln('w(',a,', ',b,', ',c,') = ',w(a,b,c)); readln(a,b,c); end; end. |
编译通过…
├ 测试数据 01:答案正确… 0ms
————————-
Accepted 有效得分:100 有效耗时:0ms
要学会用exit(x)这个东西…….
function solve(a,b,c :longint):longint;
begin
if(a<=0)or(b<=0)or(c20)or(b>20)or(c>20)then exit(solve(20,20,20));
if f[a,b,c]>0 then exit(f[a,b,c]);
if(a<b)and(b<c)then
f[a,b,c]:=solve(a,b,c-1)+solve(a,b-1,c-1)-solve(a,b-1,c)
else
f[a,b,c]:=solve(a-1,b,c)+solve(a-1,b-1,c)+
solve(a-1,b,c-1)-solve(a-1,b-1,c-1);
exit(f[a,b,c]);
end; { solve }
@Wandsea RP++,长见识……