全超时求助

P1464 Function

kkkSb_O3 @ 2023-11-26 15:53:08

#include<bits/stdc++.h>
using namespace std;
long long int n,n2,n3;
long long int f(long long int a,long long int b,long long int c)
{
    if(a<=0 or b<=0 or c<=0)
        return 1;   
    else if(a>20 or b>20 or c>20)
        return f(20,20,20);
    else if(a<b and b<c)
        return f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);
    else
        return f(a-1,b,c)+f(a-1,b-1,c)+f(a-1,b,c-1)-f(a-1,b-1,c-1);
}
int main()
{
    do
    {
        cin>>n>>n2>>n3;
        if(n!=-1 || n2!=-1 || n3!=-1)
            cout<<"w("<<n<<','<<n2<<','<<n3<<") = "<<f(n,n2,n3);
    }
    while(n!=-1 || n2!=-1 || n!=-1);
}

by zhaosihang83 @ 2023-11-29 22:16:08

建议加个记忆化搜索,不然绝对会超时


|