TLE求看

P1464 Function

IaLWH @ 2024-08-07 15:58:31

别的先不说,为什么连样例都TLE

#include<cstdio>
#define int long long

int w(int a,int b,int c){
    if(a<=0||b<=0||c<=0)
        return 1;
    if(a>20||b>20||c>20)
        return w(20,20,20);
    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);
}
signed main(){
    int i,a=0,b=0,c=0;
    scanf("%d%d%d",&a,&b,&c);
    for(i=0;a!=-1||b!=-1||c!=-1;i++){
        printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
        scanf("%d%d%d",&a,&b,&c);
    }
    return 0;
}

by Gcc_Gdb_7_8_1 @ 2024-08-07 17:09:51

AC Code:

#include <cstdio>
#include <cstring>
#define int long long

int f[25][25][25];

int w(int a,int b,int 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] != -1) {
        return f[a][b][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);
}
signed main(){
    memset(f, -1, sizeof(f));
    int i,a=0,b=0,c=0;
    scanf("%lld%lld%lld",&a,&b,&c);
    for(i=0;a!=-1||b!=-1||c!=-1;i++){
        printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,w(a,b,c));
        scanf("%lld%lld%lld",&a,&b,&c);
    }
    return 0;
}

by Gcc_Gdb_7_8_1 @ 2024-08-07 20:39:43

求帖终QAQ


by Gcc_Gdb_7_8_1 @ 2024-08-07 21:42:42

同时求互关


by Gcc_Gdb_7_8_1 @ 2024-08-08 13:02:16

@IaLWH 已关


上一页 |