toj 3761 Egg Problem (好题~~)

Egg Problem

时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交: 22 测试通过: 7

描述

 

There is a very interesting problem described as follows:

You are given two eggs.

You have access to a 100-storey building.

An egg that survives a fall can be used again.

A broken egg must be discarded.

The effect of a fall is the same for all eggs.

If an egg breaks when dropped, then it would break if dropped from a higher window.

If an egg survives a fall then it would survive a shorter fall.

It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 100th-floor windows do not cause an egg to break.

You need to figure out the highest floor an egg can be dropped without breaking. The question is how many drops you need to make.

 

Now, I want to know how to solve this problem for any number of eggs and any storeys. Can you help me?

输入

 

In the first line there is an integer T (T <= 10000), indicates the number of test cases.

In each case, there are two integers n and m (1 <= n <= 15, 1 <= m <= 100000), which are the number of eggs and storeys.

输出

 

For each case, the output format is “Case c: ans”.

c is the case number start from 1.

ans is the answer of this problem.

样例输入

 

2
2 100
3 100

样例输出

 

Case 1: 14
Case 2: 9

题目来源

2011年绍兴市赛

原理:http://blog.sina.com.cn/s/blog_6c813dbd0101bh98.html

推导过程:http://blog.csdn.net/joylnwang/article/details/6769160

 1 #include <stdio.h>
 2 int dp[20][100010];
 3 void Init(){
 4     for(int i=1; i<=15; i++){
 5         for(int j=1; j<=100000; j++){
 6             if(i == 1) dp[i][j] = j;
 7             else if(i == j) dp[i][j] = (1<<i)-1;
 8             else if(i >= j) dp[i][j] = dp[j][j];
 9             else if(i < j)  dp[i][j] = dp[i-1][j-1] + 1 + dp[i][j-1];
10             if(dp[i][j]>=100000){
11                 break;
12             }
13         }
14     }
15 }
16 int main()
17 {
18     int T;
19     Init();
20     scanf("%d", &T);
21     for(int cas=1; cas<=T; cas++){
22         int n, m;
23         scanf("%d %d", &n, &m);
24         for(int i=1; ; i++){
25             if(dp[n][i] >= m){
26                 printf("Case %d: %d
", cas, i);
27                 break;
28             }
29         }
30     }
31     return 0;
32 }
原文地址:https://www.cnblogs.com/luotinghao/p/3418427.html