最后一个测试点超时了,求大佬QAQ

P1464 Function

nbq202412904430 @ 2024-10-31 22:00:14

include<stdio.h>

include<string.h>

include<stdlib.h>

include<math.h>

long long wAw[25][25][25]; void w(long long a,long long b,long long c) {

for(int i=0;i<=a;i++)
{
    for(int j=0;j<=b;j++)
    {
        for(int h=0;h<=c;h++)
        {
            if(i==0||j==0||h==0)wAw[i][j][h]=1;
            else if(i<j&&j<h)wAw[i][j][h]=wAw[i][j][h-1]+wAw[i][j-1][h-1]-wAw[i][j-1][h];
            else wAw[i][j][h]=wAw[i-1][j][h]+wAw[i-1][j-1][h]+wAw[i-1][j][h-1]-wAw[i-1][j-1][h-1];
        }
    }
}

} int main() { long long a,b,c;

for(;;)
{
    long long a1,b1,c1;
    scanf("%lld%lld%lld",&a,&b,&c);
    if(a==-1&&b==-1&&c==-1)break;
    a1=a;
    b1=b;
    c1=c;
    if(a<=0||b<=0||c<=0)a=b=c=0;
    else if(a>20||b>20||c>20)a=b=c=20;
    w(a,b,c);
    printf("w(%lld, %lld, %lld) = %lld\n",a1,b1,c1,wAw[a][b][c]);
}

return 0;

}

这是我这个系列里第一个全凭自己写的,不舍得,求大佬指正


by 虫二bug2 @ 2024-11-08 00:13:20

@nbq202412904430 哥们这个打表不用每次都打,在循环外打表可以AC此题,在程序外打表可以冲击最优解了,用你的思路打的:

#include<bits/stdc++.h>
using namespace std;
long long a,b,c;
int f[21][21][21];
int main(){
    for(int i=0;i<=20;i++)
    for(int j=0;j<=20;j++)
    for(int k=0;k<=20;k++){
        if(i==0||k==0||j==0)f[i][j][k]=1;
        else if(i<j&&j<k)
        f[i][j][k]=f[i][j][k-1]+f[i][j-1][k-1]-f[i][j-1][k];
        else
        f[i][j][k]=f[i-1][j][k]+f[i-1][j-1][k]+f[i-1][j][k-1]-f[i-1][j-1][k-1];
    }
    while(cin>>a>>b>>c){
        if(a==-1&&b==-1&&c==-1)break;
        cout<<"w("<<a<<", "<<b<<", "<<c<<") = ";
        if(a<=0||b<=0||c<=0)a=b=c=0;
        if(a>20||b>20||c>20)a=b=c=20;
        cout<<f[a][b][c]<<endl;
    }
}

by nbq202412904430 @ 2024-11-08 08:47:38

@虫二bug2 看到了,非常感谢,好不容易自己能独立写的真的不想删掉


by 虫二bug2 @ 2024-11-08 18:35:21

@nbq202412904430 思路很好


by 虫二bug2 @ 2024-11-08 18:37:28

@nbq202412904430 题解基本都是记忆化搜索,你的做法相当于一个动归,查询是O(1)


|