UVa 10934 Dropping water balloons

来源:互联网 发布:2017剑三萝莉捏脸数据 编辑:程序博客网 时间:2024/05/02 00:07

Problem A: Dropping water balloons

It's frosh week, and this year your friends have decided that they would initiate the new computer science students by dropping water balloons on them. They've filled up a large crate of identical water balloons, ready for the event. But as fate would have it, the balloons turned out to be rather tough, and can be dropped from a height of several stories without bursting!

So your friends have sought you out for help. They plan to drop the balloons from a tall building on campus, but would like to spend as little effort as possible hauling their balloons up the stairs, so they would like to know the lowest floor from which they can drop the balloons so that they do burst.

You know the building has n floors, and your friends have given you k identical balloons which you may use (and break) during your trials to find their answer. Since you are also lazy, you would like to determine the minimum number of trials you must conduct in order to determine with absolute certainty the lowest floor from which you can drop a balloon so that it bursts (or in the worst case, that the balloons will not burst even when dropped from the top floor). A trial consists of dropping a balloon from a certain floor. If a balloon fails to burst for a trial, you can fetch it and use it again for another trial.

The input consists of a number of test cases, one case per line. The data for one test case consists of two numbers k and n, 1≤k≤100 and a positive n that fits into a 64 bit integer (yes, it's a very tall building). The last case has k=0 and should not be processed.

For each case of the input, print one line of output giving the minimum number of trials needed to solve the problem. If more than 63 trials are needed then print More than 63 trials needed. instead of the number.

Sample input

 2 10010 7865994 78659960 184467440737095516163 92233720368547758070 0

Output for sample input

1421More than 63 trials needed.6163
#include <cstdio>#include <cstring>#include <iostream>using namespace std;// record[i][j]代表用i个气球尝试j次所能测出的楼层最高高度long long record[110][65];int main(){// 算出所有答案record[0][0] = 0;record[1][0] = 0;//memset(record, 0, sizeof(record));for(int i = 1; i <= 100; i++){for(int j = 1; j <= 63; j++){record[i][j] = record[i-1][j-1] + 1 + record[i][j-1];}}long long k, n;//while(scanf("%lld %lld", &k, &n) == 2 && !(k == 0 && n == 0))while(cin >> k >> n && k){int i;for(i = 1; i <= 63; i++){if(record[k][i] >= n){printf("%d\n", i);break;}}if(i > 63)printf("More than 63 trials needed.\n");}return 0;}


该题目没有想出来,因为楼高太高,没法用数组来表示。
书上的做法是定义d(i,j)为用i个气球,尝试j次得到答案,所能测试的楼的最大高度。
假设一开始在第k层楼扔第一个气球,为了保证能在j次能得到答案。
如果气球破了,那么需要在1至(k-1)层用i-1个气球,尝试j-1得到答案。
k最大就为d(i-1,j-1)+1.
如果气球没破,那么在第k层楼之上的最大高度就为用i个气球,尝试j-1次得到答案
综上,最终的楼高应为d(i,j) = d(i-1,j-1) + 1 + d(i, j-1).
注意点是:当发现某个量不能表示进状态时(数值过大),考虑别的角度。
0 0
原创粉丝点击