RE求调

P1464 Function

Channel_Choo @ 2024-06-10 13:34:58

#include <iostream>
using namespace std;
bool flag[30][30][30] = { 0 };
long long sum[30][30][30] = { 0 };
long long W(long long a, long long b, long long c)
{
    sum[0][0][0] = 1;
    if (a <= 0 || b <= 0 || c <= 0)
    {
        flag[0][0][0] = 1;
        return 1;
    }
    else if (a > 20 || b > 20 || c > 20)
    {
        flag[20][20][20] = 1;
        sum[20][20][20] = W(20, 20, 20);
    }
    else if (a < b && b < c)
    {
        if (!flag[a][b][c - 1])
        {
            flag[a][b][c - 1] = 1;
            sum[a][b][c - 1] = W(a, b, c - 1);
        }
        if (!flag[a][b - 1][c])
        {
            flag[a][b - 1][c] = 1;
            sum[a][b - 1][c] = W(a, b - 1, c);
        }
        if (!flag[a][b - 1][c - 1])
        {
            flag[a][b - 1][c - 1] = 1;
            sum[a][b - 1][c - 1] = W(a, b - 1, c - 1);
        }
        sum[a][b][c] = sum[a][b][c - 1] + sum[a][b - 1][c - 1] - sum[a][b - 1][c];
    }
    else
    {
        if (!flag[a - 1][b][c - 1])
        {
            flag[a - 1][b][c - 1] = 1;
            sum[a - 1][b][c - 1] = W(a - 1, b, c - 1);
        }
        if (!flag[a - 1][b - 1][c])
        {
            flag[a - 1][b - 1][c] = 1;
            sum[a - 1][b - 1][c] = W(a - 1, b - 1, c);
        }
        if (!flag[a - 1][b][c])
        {
            flag[a - 1][b][c] = 1;
            sum[a - 1][b][c] = W(a - 1, b, c);
        }
        if (!flag[a - 1][b - 1][c - 1])
        {
            flag[a - 1][b - 1][c - 1] = 1;
            sum[a - 1][b - 1][c - 1] = W(a - 1, b - 1, c - 1);
        }
        sum[a][b][c] = sum[a - 1][b][c - 1] + sum[a - 1][b - 1][c] + sum[a - 1][b][c] - sum[a - 1][b - 1][c - 1];
    }
    return sum[a][b][c];
}
int main()
{
    while (true)
    {
        long long u, v, w;
        cin >> u >> v >> w;
        if (u == -1 && v == -1 && w == -1)
        {
            break;
        }
        cout << "w(a, b, c) = " << W(u, v, w) << endl;
    }
    return 0;
}

by lsortf @ 2024-06-10 14:13:12

@Channel_09 有4个错误:

1.flag数组不用开

2.if (a < b && b < c)判断部分记忆数组sum要等于这个递归函数的运算:

return sum[a][b][c] = W(a,b,c - 1) + W(a,b - 1,c - 1) - W(a,b - 1,c);
return sum[a][b][c] = W(a - 1,b,c - 1) + W(a - 1,b - 1,c) + W(a - 1,b,c) - W(a - 1,b - 1,c - 1);

3.为了防止多次调用滥用时间,判断sum是否有数,有数输出,无数存起来

4.输出错了:

cout<<"w("<<u<<", "<<v<<", "<<w<<") = "<<W(u,v,w)<<endl;

下面是改的代码:

#include <iostream>
using namespace std;
long long sum[100][100][100] = { 0 };
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);
    }if(sum[a][b][c] > 0) return sum[a][b][c];
    else if (a < b && b < c)
    {
        return sum[a][b][c] = W(a,b,c - 1) + W(a,b - 1,c - 1) - W(a,b - 1,c);
    }
    else
    {
        return sum[a][b][c] = W(a - 1,b,c - 1) + W(a - 1,b - 1,c) + W(a - 1,b,c) - W(a - 1,b - 1,c - 1);
    }
}
int main()
{
    while (true)
    {
        long long u, v, w;
        cin >> u >> v >> w;
        if (u == -1 && v == -1 && w == -1)
        {
            break;
        }
        cout<<"w("<<u<<", "<<v<<", "<<w<<") = "<<W(u,v,w)<<endl;
    }
    return 0;
}

by lsortf @ 2024-06-10 14:14:20

中间if (a < b && b < c)太复杂了,我精简了一下


by Channel_Choo @ 2024-06-10 15:34:09

@Chicken_cooked_fish 谢谢大犇帮忙! orz


by Channel_Choo @ 2024-06-10 15:55:55

@Chicken_cooked_fish 没RE了,但WA,样例也是能过得


by lsortf @ 2024-06-10 16:14:16

@Channel_09 输出错了


by lsortf @ 2024-06-10 16:14:57

cout<<"w("<<u<<", "<<v<<", "<<w<<") = "<<W(u,v,w)<<endl;

,后面有空格


by lsortf @ 2024-07-08 18:00:38

@Channel_09


by lsortf @ 2024-07-08 18:01:02

输出错了


|