为什么最后一个检测点TEL了啊

P1464 Function

WyYan @ 2024-12-13 18:40:38

//P1464 Function

#include<stdio.h>
#define ll long long

ll m[10003][4];
int len = 0;

ll w(ll a, ll b, ll c) {
    if (a <= 0 || b <= 0 || c <= 0) return 1;
    if (a > 20 || b > 20 || c > 20) return w(20,20,20);
    for (int i = 0; i < len; i++) {
        if (m[i][0] == a && m[i][1] == b && m[i][2] == c) return m[i][3];
    }
    m[len][0] = a;
    m[len][1] = b;
    m[len][2] = c;
    if (a < b && b < c)
        m[len][3] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
    else
        m[len][3] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);

    return m[len++][3];
}

int main() {
    ll a, b, c;
    scanf("%lld %lld %lld", &a, &b, &c);
    while (a != -1 || b != -1 || c != -1) {
        printf("w(%lld, %lld, %lld) = %lld\n",a,b,c, w(a, b, c));
        scanf("%lld %lld %lld", &a, &b, &c);
    }
    return 0;
}

by Qiu101029 @ 2024-12-13 18:52:53

@WyYan 《TEL》


by Qiu101029 @ 2024-12-13 18:53:56

这题要记忆化


by Qiu101029 @ 2024-12-13 18:55:50

#include<stdio.h>
using namespace std;
long long d[25][25][25];//记忆化的数组
long long a,b,c;
long long w(long long x,long long y,long long z){
    if(x<=0||y<=0||z<=0){
        return 1;
    }
    if(x>20||y>20||z>20){
        return w(20,20,20);
    }
    if(d[x][y][z]!=0){
        return d[x][y][z];
    }
    if(x<y&&y<z){
        d[x][y][z]=w(x,y,z-1)+w(x,y-1,z-1)-w(x,y-1,z);
    }else{
        d[x][y][z]=w(x-1,y,z)+w(x-1,y-1,z)+w(x-1,y,z-1)-w(x-1,y-1,z-1);
    }
    return d[x][y][z];
}
int main(){
    while(1){
        scanf("%lld%lld%lld",&a,&b,&c);
        if(a==-1&&b==-1&&c==-1){
            break;
        }
        printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,w(a,b,c));
    }
    return 0;
}

by MingDynasty @ 2024-12-13 19:00:06

在函数里分类讨论也可以的,这位


by MingDynasty @ 2024-12-13 19:00:25

#include <bits/stdc++.h>
using namespace std;
long long dp[30][30][30];
long long w(long long a, long long b, long long c)
{
    if(a <= 0 || b <= 0 || c <= 0) return 1;
    if(a > 20 || b > 20 || c > 20) return w(20, 20, 20);
    if(a<b && b<c)
    {
        if(dp[a][b][c-1] == 0)
        {
            dp[a][b][c-1] = w(a, b, c-1);
        }
        if(dp[a][b-1][c-1] == 0)
        {
            dp[a][b-1][c-1] = w(a, b-1 ,c-1);
        }
        if(dp[a][b-1][c] == 0)
        {
            dp[a][b-1][c] = w(a, b-1, c);
        }
        dp[a][b][c] = dp[a][b][c-1] + dp[a][b-1][c-1] - dp[a][b-1][c];
    }
    else
    {
        if(dp[a-1][b][c] == 0)
        {
            dp[a-1][b][c] = w(a-1, b, c);
        }
        if(dp[a-1][b-1][c] == 0)
        {
            dp[a-1][b-1][c] = w(a-1, b-1 ,c);
        }
        if(dp[a-1][b][c-1] == 0)
        {
            dp[a-1][b][c-1] = w(a-1, b, c-1);
        }
        if(dp[a-1][b-1][c-1] == 0)
        {
            dp[a-1][b-1][c-1] = w(a-1, b-1, c-1);
        }
        dp[a][b][c] = dp[a-1][b][c] + dp[a-1][b][c-1] + dp[a-1][b-1][c] - dp[a-1][b-1][c-1];
    }
    return dp[a][b][c];
}
int main()
{
    long long a, b, c;
    while(scanf("%lld%lld%lld", &a, &b, &c))
    {
        if(a==-1 && b==-1 && c==-1) break;
        printf("w(%lld, %lld, %lld) = %lld\n", a, b, c,w(a,b,c));
    }
    return 0;
}

|