tle的渣渣求助

P1464 Function

動態規劃 ```cpp var a,b,c,i,j,k:longint; f:array[-1..20,-1..20,-1..20]of int64; begin readln(a,b,c); while not((a=-1)and(b=-1)and(c=-1)) do begin fillchar(f,sizeof(f),0); write('w(',a,', ',b,', ',c,') = '); if (a<=0)or(b<=0)or(c<=0) then begin writeln(1); readln(a,b,c); continue; end else if (a>20)or(b>20)or(c>20) then begin a:=20; b:=20; c:=20; end; for i:=0 to 20 do for j:=0 to 20 do for k:=0 to 20 do if (i=0)or(j=0)or(k=0) then f[i,j,k]:=1 else if (i<j)and(j<k) then f[i,j,k]:=f[i,j,k-1]+f[i,j-1,k-1]-f[i,j-1,k] else f[i,j,k]:=f[i-1,j,k]+f[i-1,j-1,k]+f[i-1,j,k-1]-f[i-1,j-1,k-1]; writeln(f[a,b,c]); readln(a,b,c); end; end. ```
by QRcode @ 2017-03-01 20:20:29


@[徐榛的学生](/space/show?uid=34831) 得出一个值后用数组存,后面的如果数组已经存了就不用算了,避免重复算。
by PTC06 @ 2017-03-01 20:28:24


谢谢 已经过了 ```cpp #include<iostream> #include<cstdio> #include<algorithm> #include<iomanip> #include<ctime> #include<set> #include<vector> #include<list> #include<cmath> #include<cstdlib> #include<string> #include<cstring> #include<fstream> #include<queue> #include<stack> #include<cctype> #define l long long using namespace std; l a[21][21][21]; void p() { for(int i=0;i<21;++i) for(int j=0;j<21;++j) for(int n=0;n<21;++n) { if(i<=0||j<=0||n<=0) a[i][j][n]=1; else if(i>20||j>20||n>20) a[i][j][n]=a[20][20][20]; else if(i<j&&j<n) a[i][j][n]=a[i][j][n-1]+a[i][j-1][n-1]-a[i][j-1][n]; else a[i][j][n]=a[i-1][j][n]+a[i-1][j-1][n]+a[i-1][j][n-1]-a[i-1][j-1][n-1]; } } int main() { p(); l aa,b,c,ans; while(1) { cin>>aa>>b>>c; if(aa==-1&&b==-1&&c==-1) break; if(aa<0||b<0||c<0) ans=1; else if(aa>20||b>20||c>20) ans=a[20][20][20]; else ans=a[aa][b][c]; cout<<"w("<<aa<<", "<<b<<", "<<c<<") = "<<ans<<endl; } return 0; } ```
by sbcxknmsl @ 2017-03-01 21:02:34


他叫你记忆搜索还不记忆搜索,这不作死吗
by tumult_2 @ 2017-04-05 19:37:36


这耗时那么多,为什么不记忆化搜索? 作死的节奏啊。
by howtolearn @ 2017-05-19 20:59:17


|