Solution to a problem P1464 [Function]

See according to Yu, the difficulty of the problem is that the search and storage memory data structure.


\ [First: \ text {storage variable} \]
Since \ (W \) function takes three parameters: \ (A, B, C \) . Therefore, we can set up an array of \ (f \) , that \ (f \) is set to a few dimensions of it? I think it should be set to three-dimensional, that \ (f [] [] [] \) , and then observe the data range:
\ [\ text {Enter the number of guarantee between [-9223372036854775808,9223372036854775807]} \]
What? Range so big? ? It stands to reason that should be set in terms of a imagined such a large array:
\ [f [9223372036854775807] [9223372036854775807] [9223372036854775807] \]

It has long been violent space ( \ (MLE \) ), andThey might \ (luogu \) evaluation machine broken mess things up for youAnd enter the number as well as negative, \ (∴ \) This data storage method is wrong drop \ (! \)

? How was the correct way to store data do
we take a look at the topic:

If $ a \ leq 0 or b \ leq 0 or c \ leq 0 $ return value \ (1 \) .
If \ (a> 20 or b> 20 or c> 20 \) returns \ (w (20, 20, 20) \)

Therefore, we can give the following initialized:
\ (1 \) when $ a \ leq 0 or b \ leq 0 or c \ when leq 0 $, direct \ (COUT <<. 1 \) .
\ (2 \) when \ (a> 20 or b> 20 or c> 20 \) , executing \ (w (20,20,20) \) and \ (COUT \) it.
\ (3 \) other cases directly \ (cout << w (a,
b, c) \) According to the analysis: it shows: \ (f \) array requires only the largest open \ (21 \) , because my personal preference , opened \ (25 \) a \ (QAQ \) .
Code shows the code portion realization \ (↓↓↓ \)

while(cin>>a>>b>>c) //不断输入a,b,c
{
    if(a==-1 && b==-1 && c==-1) return 0;  //输入结束
    else if (a<=0 || b<=0 ||c<=0 ) cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<1<<endl;
    else if(a>20 || b>20 ||c>20) cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(20,20,20)<<endl;
    else cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
}

\ [Second: \ text {search} Memory \]
violent Search is very simple, the meaning of a sentence in accordance with the title fight is like, do not explain, given the code:

inline int w(int a,int b,int c)
{
    if(a<=0 ||b<=0 ||c<=0) return 1;
    else if(a>20 ||b>20 ||c>20) return w(20,20,20);
    else if(a<b && b<c) return w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c-1);
    else f[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);
}

This question is crucial is that the memory of the search , in fact, we do not want to search the memory of so profound, the \ (f \) array into the function can (do not forget the \ (f \) array initialization), the following specific circumstances :

inline int w(int a,int b,int c)
{
    if(f[a][b][c]!=-1) return f[a][b][c];
    if(a<=0 ||b<=0 ||c<=0) return f[a][b][c]=1;
    else if(a>20 ||b>20 ||c>20) return f[a][b][c]=w(20,20,20);
    else if(a<b && b<c) return f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c-1);
    else return f[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);
}


memset(f,-1,sizeof(f)); //(在 int main()中

This problem also lies in a pit box output , due to this reason I downloaded the data once (I am too weak QAQ),I wish you all and I do not step on the same pitWell this solution to a problem so far. Give a final code as follows \ (: \)

#include<bits/stdc++.h>  //无敌的万能头
using namespace std;
int f[25][25][25];
inline int w(int a,int b,int c)
{
    if(f[a][b][c]!=-1) return f[a][b][c];
    if(a<=0 ||b<=0 ||c<=0) return f[a][b][c]=1;
    else if(a>20 ||b>20 ||c>20) return f[a][b][c]=w(20,20,20);
    else if(a<b && b<c) return f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c-1);
    else return f[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);
}
int main()
{
    long long a,b,c;   //别忘开long long
    memset(f,-1,sizeof(f));
    while(cin>>a>>b>>c)
    {
        if(a==-1 && b==-1 && c==-1) return 0;
        else if (a<=0 || b<=0 ||c<=0 ) cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<1<<endl;
        else if(a>20 || b>20 ||c>20) cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(20,20,20)<<endl;
        else cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
    }
    return 0;//完美的结束QAQ
}

Guess you like

Origin www.cnblogs.com/Luke-Skywalker/p/11227528.html