求助,永远的超时

P1464 Function

fff1842889002 @ 2022-04-17 17:16:31

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll s[30][30][30];
ll w(ll a,ll b,ll c)
{
    if(a<=0||b<=0||c<=0) return 1;
    else if(a>20||b>20||c>20) s[a][b][c]= w(20,20,20);
    else if(a<b&&b<c)  s[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    else s[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
    return s[a][b][c];
}
int main()
{
    ll a,b,c;
    while(1)
    {
        cin>>a>>b>>c;
        if(a==-1&&b==-1,c==-1) break;
        ll ans;
        if(a>20) a=25;
        if(b>20) b=25;
        if(c>20) c=25;
        if(!s[a][b][c]) ans=w(a,b,c);
        else ans=s[a][b][c];
        cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<ans<<endl;
    }
    return 0;
}

by Fast_Gai_Tle @ 2022-04-17 17:37:31

@fff1842889002 加一个如果s[a][b][c]被算过就直接返回就行了


by muhouchen @ 2022-04-28 22:43:05

@fff1842889002 对的,加到循环的第二步


|