调完还是tle 我心已死

P1464 Function

__oio__ @ 2024-04-14 10:01:20

#include <bits/stdc++.h> 
using namespace std;

long long dp[100][100][100];
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)
    {
        if(dp[20][20][20] != 0)
        {
            return dp[20][20][20];
        }
        dp[20][20][20] = w(20,20,20);
        return dp[20][20][20];
    }
    if (a < b&&b < c)
    {
        if(dp[a][b][c] != 0)
        {
            return dp[a][b][c];
        }
        dp[a][b][c] = w(a,b,c-1) + w(a,b-1,c-1) + w(a,b-1,c);
        return dp[a][b][c];
    }
    else
    {
        if(dp[a][b][c] != 0)
        {
            return dp[a][b][c];
        }
        dp[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 dp[a][b][c];
    }   
}
int main()
{
    long long a,b,c;
    while (1)
    {
        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 guoziqi201010 @ 2024-04-14 10:13:25

你看看我的理解一下:

#include<bits/stdc++.h>
using namespace std;
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;
    }
    else if (a > 20 || b > 20 || c > 20)
    {
        return w(20, 20, 20);
    }
    else if (f[a][b][c] != 0)
    {
        return f[a][b][c];
    }
    else if (a < b && b < c)
    {
        f[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
    }
    else
    {
        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);
    }
    return f[a][b][c];
}
int main()
{
    long long a, b, c, tmp[9999], tmpa[9999], tmpb[9999], tmpc[9999];
    int i = 0;
    while (cin >> a >> b >> c)
    {
        if (a == -1 && b == -1 && c == -1)
        {
            break;
            return 0;
        }
        cout << "w(" << a << ", " << b << ", " << c << ") = ";
        cout << w(a, b, c) << endl;
        /*tmp[i] = w(a, b, c);
        tmpa[i] = a;
        tmpb[i] = b;
        tmpc[i] = c;
        i++;*/
    }
    /*for (int j = 0; j < i; j++)
    {
        cout << "w(" << tmpa[j] << ", " << tmpb[j] << ", " << tmpc[j] << " )= ";
        cout << tmp[j]<<endl;
    }*/

    return 0;
}

by CZY880201 @ 2024-04-14 10:15:48

开记忆化


by __oio__ @ 2024-04-20 15:36:12

@guoziqi201010 十分感谢
鞠躬了


|