湖南中医药大学2017年集训队第四场选拔赛-Problem B: Power Eggs

Problem B: Power Eggs

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 74 Solved: 35
[Submit][Status][Web Board]
Description

Benedict bought K identical power eggs from Dropeggs.com, and now he wants to test them by dropping them from different floors of his building. His building has N floors numbered 1 to N. F is an unknown number in the range from 0 to N, inclusive. Each egg will break if dropped from floor F+1 or above, but will not break if dropped from floor F or below. Benedict can drop each egg as many times as he wants from any floor until it breaks. He wants to know the minimum number of egg drops necessary to ensure that he can determine F.

For example, if there are three floors and Benedict has only one egg, then he has to first throw the egg from the first floor, then from the second floor (if the egg survived), and then from the third floor (if the egg survived). Therefore, three drops are required in the worst case.

Input

The first line contains one number T (1 ≤ T ≤ 10000) which is the number of test cases, followed by T lines. Each of the next T lines contains two numbers: N, the number of floors (1 ≤ N ≤ 2000000007) and K, the number of eggs (1 ≤ K ≤ 32).
Output

For each of the T lines, print the minimal number of drops required, or if it’s greater than 32, print the word Impossible. After that many drops, Benedict gets too tired and cannot continue.
Sample Input

4
10 1
100 2
30 30
2000000000 2
Sample Output

10
14
5
Impossible

[分析]
做过类似的题目。动态规划。

先说一下原理。
比如有100楼,你只有一个蛋,那么你只能用一个蛋来遍历,找到最大楼层,所以最坏情况是100;

如果有100楼,你有很多蛋(这里的很多是指足够你二分法浪费的个数),那么最快找到楼层的方法就是搜索的二分法了。(至于是几次就不算啦hhh,懒)

如果有100楼,而你只有两个蛋,还是用二分法吗?50楼掉落,碎则从1楼遍历到49楼,不碎则从51楼遍历到100楼,明显地,这和sample里面input100 2,output 14有着天差地别。那么应该怎么做呢?

现在讲的是一个更优但依旧错误的方法。依旧100楼,有2个蛋。将楼10等分,分别在10楼20楼30楼….90楼100楼扔下第一个鸡蛋。什么意思呢,举个例子:
假设鸡蛋在54楼碎。那么我们在10楼20楼30楼40楼50楼依次扔下第一个鸡蛋,都没碎。在60楼它碎了,所以目标楼层肯定在51到59之间。这时我们只剩一个蛋,所以只好遍历。我们依次从51,52,53楼扔下,都没碎。54楼碎了。所以测试了10次。
所以用这种方法测试100楼2个蛋的最坏情况是18次(10,20,30,40,50,60,70,80,90,100,91,92,93,94,95,96,97,98,99共18)
虽然情况变好了,但是18和14还有4次的差别。怎么办呢?

这时候就想到了动态规划。100 2只是其中的一个子问题。
先给出状态方程:dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
下图是dp矩阵
这里写图片描述
解释一下状态方程:i是剩余的测试次数,j是剩几个蛋,dp[i][j]是最大楼层数。
对dp矩阵的第一行第一列赋值,dp[1][j]无论剩几个蛋,因为只有一次机会,所以只能确定2个楼层。dp[i][1]当前剩i次,只有一个蛋,所以最大楼层就是用尽测试次数都不碎,所有有i+1层。
然后利用状态方程更新矩阵。
当在某个子局面时,可能碎或者不碎。碎就是dp[i-1][j-1],不碎就是dp[i-1][j],所以这个子局面就是碎与不碎分开的两种情况的相加。
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];

[代码]

#include <cstdio>  
long long dp[33][33];

void solve()
{
    long long n, m;
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i < 33; i++)
    {
        if (dp[i][m] >= n + 1)
        {
            printf("%d\n", i);
            return;
        }
    }
    printf("Impossible\n");
}

int main()
{
    for (int i = 1; i < 33; i++)
    {
        dp[i][1] = i + 1; 
        dp[1][i] = 2;
    }
    for (int i = 2; i < 33; i++)
    {
        for (int j = 2; j < 33; j++)
        {
            dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
        }
    }
    int t; 
    scanf("%d", &t);
    while (t--)
    {
        solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值