听取TLE声一片0pts 求救

P1464 Function

kkksc24 @ 2024-11-28 21:53:00

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

by lizexi707 @ 2024-11-29 16:02:17

递归少了一个判断,应当在

if(a>20 || b>20 || c>20)
    {
        return w(20,20,20);
    }

加一个

    if(f[a][b][c]!=0)
        return f[a][b][c];

以下是代码:

using namespace std;
long long a,b,c;
long long f[25][25][25];
long long w(long long a,long long b,long long c)
{
    if(a<=0||b<=0||c<=0)
    {
        return 1;
    }
    if(a>20 || b>20 || c>20)
    {
        return w(20,20,20);
    }
    if(f[a][b][c]!=0)
        return f[a][b][c];

    if( a<b &&b<c)
    {
        return f[a][b][c] =w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);

    }
    return f[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);
}
int main()
{
    while(1){
        cin>>a>>b>>c;
        if(a==-1&&b==-1&&c==-1) 
            return 0;
        cout<<"w("<<a<<", "<<b<<", "<<c<<")"<<" = "<<w(a,b,c)<<endl;

    }

    return 0;
}

by kkksc24 @ 2024-12-04 20:57:48

@lizexi707 记录 AC已玄关


|