60分,求助!

P1464 Function

guaiguaitu @ 2023-07-02 15:12:01

#include<bits/stdc++.h>
using namespace std;
long long book[50][50][50];
long long w(long long a1,long long a2,long long a3){
    if(a1<=0||a2<=0||a3<=0) return 1;
    else if(a1>20||a2>20||a3>20) book[20][20][20]=w(20,20,20);
    else if(book[a1][a2][a3]) return book[a1][a2][a3];
    else if(a1<a2&&a2<a3) book[a1][a2][a3]=w(a1,a2,a3-1)+w(a1,a2-1,a3-1)-w(a1,a2-1,a3);
    else book[a1][a2][a3]=w(a1-1,a2,a3)+w(a1-1,a2-1,a3)+w(a1-1,a2,a3-1)-w(a1-1,a2-1,a3-1);
    return book[a1][a2][a3];
}
int main(){
    long long a,b,c;
    memset(book,0,sizeof(book));
    while(cin>>a>>b>>c){
        if(a==-1&&b==-1&&c==-1) return 0;
        cout<<"w("<<a<<", "<<b<<", "<<c<<") = ";
        if(a>20||b>20||c>20){
            cout<<1048576<<endl;
            continue; 
        }
        long long ans=w(a,b,c);
        cout<<ans<<endl;
    }
    return 0;
}

by xinglili @ 2023-07-04 23:33:42

一:else if(a1>20||a2>20||a3>20) book[20][20][20]=w(20,20,20); 改为 else if(a1>20||a2>20||a3>20) return w(20,20,20); 因为w(100,100,100)这种值等于w(20,20,20)但是写上去就直接越界了,所以直接return w(20,20,20)。

二: if(a>20||b>20||c>20){ cout<<1048576<<endl; continue; }这一段删掉,因为w(21,-21,21)按题意啊a,b,c其中一个为负就应该return 1


by guaiguaitu @ 2023-07-06 11:40:13

@xinglili 谢谢大佬,我已经过了


|