为什么记忆化也会TLE

P1464 Function

jiezecheng @ 2024-09-08 11:55:11

我个人觉得自己理解了题解,但是这个代码怎么调都是 Subtack 1 两个点全 TLE ,剩下全对

#include<bits/stdc++.h>
using namespace std;
int ans[25][25][25];
int w(long long a,long long b,long long c);
int find(long long a,long long b,long long c){
    if(ans[a][b][c]==0){
        ans[a][b][c]=w(a,b,c);
    }
    return ans[a][b][c];
}

int w(long long a,long long b,long long c){
    if(a<=0 or b<=0 or c<=0){
        return 1;
    }
    if(a>20 or b>20 or c>20){
        if(ans[20][20][20]!=0){
            return ans[20][20][20];
        }else{
            return w(20,20,20);
        }
    }
    if(a<b and b<c){
        ans[a][b][c]=find(a,b,c-1)+find(a,b-1,c-1)-find(a,b-1,c);
        return ans[a][b][c];
    }
    ans[a][b][c]=find(a-1,b,c)+find(a-1,b-1,c)+find(a-1,b,c-1)-find(a-1,b-1,c-1);
    return ans[a][b][c];

}
int main(){
    while(1){
        int a,b,c;
        cin>>a>>b>>c;
        if(a==-1 and b==-1 and c==-1) return 0;
        cout<<"w("<<a<<", "<<b<<", "<<c<<')'<<" = "<<w(a,b,c)<<endl;
    }
} 

by jiezecheng @ 2024-09-08 11:55:46

求助大佬帮忙


by kaiweiwang @ 2024-09-08 12:02:58

一年前的代码,已经忘了怎么写的了

#include<cstdio>
#include<cstring>
#define llx long long
using namespace std;
llx f[40][40][40],a,b,c;
llx fun(llx x,llx y,llx z){ 
    if(x<=20&&y<=20&&z<=20&&x>=0&&y>=0&&z>=0)if(f[x][y][z]){return f[x][y][z];}
    if((x<=0)||(y<=0)||(z<=0)){return 1;}
    if((x>20)||(y>20)||(z>20)){return fun(20,20,20);}
    if((x<y)&&(y<z)){return f[x][y][z]=fun(x,y,z-1)+fun(x,y-1,z-1)-fun(x,y-1,z);}
    return f[x][y][z]=fun(x-1,y,z)+fun(x-1,y-1,z)+fun(x-1,y,z-1)-fun(x-1,y-1,z-1);
}
void out(){
    printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,fun(a,b,c));
}
int main(){
    while(1){
        scanf("%lld%lld%lld",&a,&b,&c);
        if((a==-1)&&(b==-1)&&(c==-1)){
            break;
        }
        out();
    }
    return 0;
}

求关


by liujinxv123 @ 2024-09-08 12:26:00

建议O3启动?


by mahaorui2012 @ 2024-09-15 16:21:24

int ans[25][25][25]

改成

long long ans[25][25][25]


by jiezecheng @ 2024-09-21 08:28:43

@mahaorui2012 好了,谢谢


by XY_guiling_ @ 2024-10-12 20:05:59

加一个

#define int long long

就行了


|