蒟蒻记忆化全WA求助、

P1464 Function

achjuncool @ 2022-06-18 18:49:13

样例是对的,也不会爆(应该

代码如下:::::::

#include <iostream>
using namespace std;
long long x, y, z, r[25][25][25];
long long func(long long a, long long b, long long c){
    if(a <= 0 || b <= 0 || c <= 0){
        return 1;
    } else if(r[a][b][c] != 0){
        return r[a][b][c];
    } else if(a > 20 || b > 20 || c > 20){
        r[a][b][c] = func(20, 20, 20);
    } else if(a < b && b < c){
        r[a][b][c] = func(a, b, c - 1) + func(a, b - 1, c - 1) - func(a, b - 1, c);
    } else{
        r[a][b][c] = func(a - 1, b, c) + func(a - 1, b - 1, c) + func(a - 1, b, c - 1) - func(a - 1, b - 1, c - 1);
    }
}
int main() {
    cin >> x >> y >> z;
    while(x != -1 && y != -1 && z != -1){
        if(x > 20) x = 21;
        if(y > 20) y = 21;
        if(z > 20) z = 21;
        cout << "w(" << x << ", " << y << ", " << z << ") = " << func(x, y, z) << endl;
        cin >> x >> y >> z;
    }
    return 0;
}

by StarLbright40 @ 2022-06-18 19:11:22

首先,您的函数在后三种情况下都没有返回值

其次,应该先判断是否有数超过 20 再判断是否记忆过这个函数值,否则还是会爆


by _maojun_ @ 2022-06-18 19:13:30

你不能把x,y,z先减了再输出啊……


by achjuncool @ 2022-06-18 19:14:10

@星光0000 第一个是我的错,但是第二个,我再输入那里加了这几行:

if(x > 20) x = 21;
if(y > 20) y = 21;
if(z > 20) z = 21;

还有那个,我样例输出是对的,不知道为什么


by _maojun_ @ 2022-06-18 19:14:57

while里应改为

printf("w=(%lld,%lld,%lld)=%lld\n",x,y,z,func(min(21,x), min(21,y), min(21,z)));

by _maojun_ @ 2022-06-18 19:15:25

@achjuncool


by achjuncool @ 2022-06-18 19:15:56

@maojun 啊这有道理我改下


by _maojun_ @ 2022-06-18 19:17:10

哦对,min里面要的21要加ll

func( min(21ll,x),min(21ll,y),min(21ll,z) )

by _maojun_ @ 2022-06-18 19:30:47

@achjuncool 还有就是

x,y,z,都可能为-1,只有全是-1时才能结束,所以应该用或逻辑;

x,y,z都可能是大负数,所以应该\max(0,x)

for(scanf("%lld%lld%lld",&x,&y,&z);x!=-1||y!=-1||z!=-1;scanf("%lld%lld%lld",&x,&y,&z))
        printf("w(%lld, %lld, %lld) = %lld\n",x,y,z,dfs(min(21ll,max(0ll,x)), min(21ll,max(0ll,y)), min(21ll,max(0ll,z))));

by _maojun_ @ 2022-06-18 19:32:07

@achjuncool 所以你最后还是没有返回值……


by achjuncool @ 2022-06-18 19:39:41

@maojun 你这玩意我看着有点头晕((不是我一会会看得懂的((


| 下一页