Description |
Recently, XX held a strange competition: the king of the hardness of eggs. Contestants are hens from all over the world. The competition content is to see who has the hardest eggs. What's more strange is that XX does not use any precision instruments to measure the hardness of eggs, they used the oldest method-throwing eggs from the height-to test the hardness of the eggs. If the eggs under the hen fell from layer a of the tall building, however, when the layers a + 1 are broken, the hardness of the hen's eggs is. Of course, you can find various reasons to prove that this method is not scientific. For example, the egg hardness under the same hen may be different, but this does not affect the competition of XX Company, because they only want to attract everyone's attention, when an egg falls down from the top of the fifth floor, this situation can still attract many people to stop watching. Of course, XX Company will never forget to put a picture on the upper floor and write the words "XX Company"-this competition is just an alternative advertisement of XX Company. A diligent thinker can always find A mathematical problem from one thing, which is no exception. "If there are A lot of eggs with the same hardness, I can use A binary method to measure the hardness of the eggs with the least number of times." Mr. A was very satisfied with his conclusion, but soon the trouble came. "But if I don't have enough eggs, for example, I only have one, then I have to throw one layer from the first floor, in the worst case, I have to throw it 100 times. If you have 2 eggs, you can throw them from the ground up on the second floor ...... Wait, no. It seems that it should start from 1/3 ...... 3 eggs, 4, 5, more ......", As usual, Mr. A is stuck in another mental deadlock, so he prefers to look for trouble rather than thinking diligently. Okay, now that you have trouble, someone has to solve it. You can solve the problem of small A by yourself :) |
About Input |
The input includes multiple groups of data. Each group contains one row of data and two positive integers (n and m) (1 <= n <=, 1 <= m <= 10). n indicates the height of the building, m indicates the number of eggs you have now. These eggs have the same hardness (that is, they both fall down from the same height or are not broken) and are less than or equal to n. You can assume that the eggs whose hardness is x will not be broken from the place where the height is less than or equal to x (the eggs that are not broken can be used again ), but as long as you throw from a place higher than x, it will be broken. For each group of input data, you can assume that the hardness of the eggs is between 0 and n, that is, the layers of n + 1 will be broken. |
About output |
For each group of input, an integer is output, indicating the number of times the best policy is used to throw eggs in the worst case. |
Example Input |
100 1100 2 |
Example output |
10014 |
Prompt |
The optimal strategy refers to the strategy that requires the least number of eggs to be thrown in the worst case. If there is only one egg, you can only start from the first layer. In the worst case, the hardness of the egg is 100, so you need to throw it 100 times. If you adopt other policies, you may not be able to measure the hardness of the eggs (for example, if you threw the eggs at the second layer for the first time and the results are broken, you cannot determine whether the hardness is 0 or 1 ), in the worst case, you need to throw an infinite number of times, so the answer to the first group of data is 100. |
|
# Include <stdio. h> # include <stdlib. h> int max (int x, int y) {return (x> y )? X: y;} int f (int n, int m)/* Number of times required for m eggs on n floors */{if (m = 1) return n; /* one egg can only be tested on one layer from the first floor. It can take up to n times */else if (n <= 1) return n; /* a maximum of one floor can be thrown once */else {static int answer [101] [11]; /* used to store the calculated results */if (answer [n] [m])/* if it has been computed and stored, it is directly read, no need to calculate again */{return answer [n] [m];} else {int times = 9999; int j; for (j = 1; j <= n; j ++)/* consider all policies and select the optimal solution */{int x = f (j-1, m);/* if not broken, then use the m eggs to test the (J-1) Layer */int y = f (n-j, m-1);/* If the eggs are broken, then test the (n-j) layer x/int z = 1 + max (x, y) above with m-1) eggs ); /* consider the worst case */if (z <times)/* Find a better policy */times = z;} return answer [n] [m] = times; /* store the calculation results for the next Direct Read */} int main () {int n = 100, m = 2; while (scanf ("% d ", & n, & m) = 2) {printf ("% d \ n", f (n, m);} return 0 ;} |