I have read a lot of people's report on how to solve this problem. I can't bear the style of their code. Most of the Code on the internet is brute force-enumeration. I have no idea, but I have to say that this is a simple method. I tried my best to optimize the speed and code readability. The original code running speed was (1 5 10) -- after 3.5 seconds, it changed to about 2 seconds. Now I can only think of this, I did not think of other optimization methods. I will take this as the official version for the time being. It is estimated that there will not be much improvement in the future. If there is a better method, I will sort it out in time.
/×
2507: Li Bai
Description
Li Bai, the great poet, enjoys a good life. Fortunately, he never drives.
One day, he carried his wine pot and came out from home. There were two drinks in the wine pot. While singing:
There is no way to go in the street, and bring up a pot to drink.
Double the number of shopping malls, and the amount of flowers and drinks.
On this road, he met the store m times and spent N times. It is known that the last time he met the flower, he just drank the wine.
Calculate the order of the stores and flowers that Li Bai encounters. You can mark the stores as A and the flowers as B.
For example, on this road, he met the store five times and spent ten times. It is known that the last time he met the flower, he just drank the wine. Then: babaabbabbabbbb is a reasonable order. How many answers are there like this? Calculate the number of all possible solutions (including the number given by the question ).
Input
Enter an integer T, including the T group data. Each group of data includes the number of times that a store encounters m and the number of times that it spends n.
Output
For each group of data, output the number of possible orders of stores and flowers that Li Bai encounters.
Sample Input
1 5 10
Sample output
14
*/
Original code:
# Include <stdio. h>
Int sum; // possible number
Int main ()
{
Int T;
Scanf ("% d", & T );
While (t --) // number of case groups
{
Sum = 0;
Int M, N; // m represents a hotel, and N represents a flower.
Scanf ("% d", & M, & N );
Void Achol (int A, int M, int N); // recursive function declaration
Achol (2, m, n); call
Printf ("% d \ n", sum );
}
Return 0;
}
Void Achol (int A, int M, int N)
{
If (a> 0) // skip this line
{
If (M> 0) meets the hotel
Achol (A * 2 m-1, N );
If (n> 0) // encounter flowers
Achol (A-1, M, n-1 );
}
Else if (A = 0 & M = 0 & n = 0) // judge
Sum + = 1;
}
Optimized Code:
# Include <stdio. h>
Int sum; // possible number
Int main ()
{
Int T;
Scanf ("% d", & T); // number of input data groups
While (t --)
{
Sum = 0; // The number of possible input data types is cleared each time.
Int M, N; // m represents a hotel, and N represents a flower.
Scanf ("% d", & M, & N );
Void Achol (int A, int M, int N );
Achol (2, m, n );
Printf ("% d \ n", sum );
}
Return 0;
}
Void Achol (int A, int M, int N)
{
If (M> 0 & N & A) // there are only two possibilities, either hotel or flower.
Achol (A * 2 m-1, N );
If (n> 2 &)
Achol (A-1, M, n-1 );
If (A = 2 & M = 0 & n = 2) // the last two are flowers and can be verified by yourself.
Sum + = 1;
}
In this Code segment, I still have some questions:
1. If all the content of a recursive function is written as follows:
If (a> 0)
{
If (M> 0 & N) // there are only two possibilities, either hotel or flower.
Achol (A * 2 m-1, N );
If (n> 2)
Achol (A-1, M, n-1 );
If (A = 2 & M = 0 & n = 2) // the last two are flowers and can be verified by yourself.
Sum + = 1;
}
In theory, the limit of a can be changed to a> = 2. However, after the change, the limit will not work. sum will always be zero. Please kindly point it out if I think about it;
II:
On the basis of "1", I can add a limit on m, a * 2 <= N, that is, the total number of wines cannot exceed N, which can be solved before recursion. However, an error is also reported when the value is added.
Blue Bridge cup-Li Bai liquor (official version)