This article is reproduced from http://blog.csdn.net/shuangde800/article/details/11273123
Test instructions
You have K-like water polo, test on an N-storey building, and you want to know that water polo can be broken down from several floors. Because you are lazy, you want to throw at least water polo to measure the lowest floor where the water polo just breaks down. (In the worst case, the water polo will not break on the top floor) you can drop the water polo on one floor and test it, if the water polo is not broken, you can pick it up and continue to use it.
Input
Each line you enter contains multiple sets of tests, one for each group of tests. Each set of tests consists of two integers k and n, 1 <= k <= 100 and N is a 64-bit integer (yes, the building is really high), and the last set of k = 0,n=0 represents the end, and this set of tests is not processed.
Output
For each test, the output, in the worst case, measured the minimum number of times the water polo broke down the floor. If he is more than 63 times, output "more than trials needed."
Ideas
The topic has been made very clear, in the worst case (that is, the last layer can be broken, but you do not know is the last layer), with the fewest number of times can be known.
Assuming you have countless water polo cases, then the least number of times is definitely a two-point approach: First fall in the middle, if broken, indicating the target bit
Placed in the lower part, not broken words are indicated in the upper half. Continue on the corresponding part of the next two points down ... Need Logn times.
But the number of water polo is limited, for example, only one water polo case, you directly on the middle floor down, if broken, then you have no other
The ball went on to do the experiment. So you have to go up from the first floor, and the first broken floor is the target floor. The worst case scenario is to do it n times.
This is still not very good to think, so to change the question, into: "Give K balloons, throw J times, the most can determine the first layer?" ”
In this way, the F (i, j) state can be used to represent the I-balloon, and the maximum number of layers to be determined by throwing J times.
For f (i, j), we don't know that it can determine the number of layers at most, assuming that the ball is still in the X layer for the first time, and if the ball is broken, then we spend a ball and still a cost,
We can know that f (i-1, j-1) can determine the number of layers, then you can determine x = f (i-1, j-1) + 1.
If the ball is not broken in the X-layer, then we only spend the cost, and I have the ball, j-1 times can be used, then with these can be determined the number of layers F (i, j-1)
So, get the recursive type, roll up the highest number of layers that can be determined:
F (i, j) = f (i-1, j-1) + 1 + f (i, j-1);
Code:
/************************************************************************* > File name:4.cpp > A Uthor:howe_young > Mail: [email protected] > Created time:2015 August 15 Saturday 16:47 41 sec ************************************************************************/#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespaceStd;typedefLong Longll;Const intMAXN = the; ll DP[MAXN][MAXN];voidinit () {memset (DP,0,sizeof(DP)); for(inti =1; I < -; i++) for(intj =1; J < -; J + +) Dp[i][j]= Dp[i-1][j-1] + dp[i][j-1] +1;}voidprint () { for(inti =1; I < -; i++) { for(intj =1; J < -; J + +) cout<< Dp[i][j] <<" "; cout<<Endl; }}intMain () {init (); intK; ll N; while(~SCANF ("%d%lld", &k, &n) &&k) {k= Min ( the, K); BOOLFlag =false; for(inti =1; I < -; i++) { if(Dp[k][i] >=N) {flag=true; printf ("%d\n", i); Break; } } if(!flag) puts ("More than trials needed."); } return 0;}
View Code
UVA 10934 dropping Water balloons (reprint)