求助,100分,有一个RE了

P1464 Function

wsbSB @ 2023-07-16 10:13:50

#include<iostream>
#include<cstdio>
long long n1[500][4],n2[500][4];
int t[25][25][25];
int w(int a,int b,int c)
{
    if(a<=0||b<=0||c<=0)return 1;
    else if(t[a][b][c]!=-10000)return t[a][b][c];
    else if(a>20||b>20||c>20)t[a][b][c]=w(20,20,20);
    else if(a<b&&b<c) t[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    else t[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);
    return t[a][b][c];
}
using namespace std;
int main()
{
    int i,j,k;
    for(i=1;i<=24;i++)
    for(j=1;j<=24;j++)
    for(k=1;k<=24;k++)
    t[i][j][k]=-10000;
    for(i=1;;i++)
    {
        scanf("%lld%lld%lld",&n1[i][1],&n1[i][2],&n1[i][3]);
        n2[i][1]=n1[i][1]; 
        n2[i][2]=n1[i][2];
        n2[i][3]=n1[i][3];
        if(n1[i][1]>20)n1[i][1]=21;
        if(n1[i][2]>20)n1[i][2]=21;
        if(n1[i][3]>20)n1[i][3]=21;
        if(n1[i][1]==-1&&n1[i][2]==-1&&n1[i][3]==-1)
        { 
            i--;
            break;
        }
    }
    for(j=1;j<=i;j++)
    printf("w(%lld, %lld, %lld) = %d\n",n2[j][1],n2[j][2],n2[j][3],w(n1[j][1],n1[j][2],n1[j][3]));
    return 0;
     }

by WangSH2012 @ 2023-07-18 21:28:45

题解


by Sirius6699 @ 2023-07-28 11:00:33

#include<bits/stdc++.h>//这题要使用记忆化搜索 不然超时
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//关闭同步流 一个好习惯
using namespace std;
const int maxn=1e3+5;
//#define int long long 最好不要用 否则int main()要改成signed main()
int vis[30][30][30],ans[30][30][30];//俩记忆化储存要用的数组
int w(long long a,long long b,long long c)//一定要long long
{
/*----------------------开始记忆化储存----------------------*/
    if(a<=0||b<=0||c<=0)
    {
        return 1;
    }
    else if(a>20||b>20||c>20)
    {
        return w(20,20,20);
    }
    else if(vis[a][b][c]==1)
    {
        return ans[a][b][c];
    }
    else if(a<b&&b<c)
    {
        ans[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    }
    else
    {
        ans[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);
    }
    vis[a][b][c]=1;
    return ans[a][b][c];
}
/*----------------------开始敲主函数----------------------*/
int main()
{
    long long a,b,c;
    while(1)
    {
        cin>>a>>b>>c;
        if(a==-1&&b==-1&&c==-1)
        {
            return 0;
        }
        cout<<"w"<<"("<<a<<", "<<b<<", "<<c<<")"<<" = "<<w(a,b,c)<<endl;//输出格式很严格 打字千万不要错
    }
/*--------------------------------------------------------*/
    return 0;//好习惯 不能丢
}

|