power egg dp问题

Problem B: Power Eggs

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 54  Solved: 23
[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

HINT

————————————————————————————————————————————————————————

  丢蛋这个问题让我接触到了dp世界的新的大门,似乎我以前学的dp是假dp.

  要解决这个问题,首先得从丢蛋这个问题说起,大概意思就是给你一个楼高和一个蛋的数量,你要给出最差情况下找出其从几楼下去而不碎的最小次数,这个最差情况有点难以理解,我姑且理解成,一切不按照逻辑乱来的行为(俗称碰运气)都会带来最坏的结果(即你损失一个蛋的同时并不会得到什么有用的信息)

  那么仔细想想,一个蛋的时候,只能从1楼开始扔起,然后扔到蛋碎为止,就知道蛋的坚硬程度了,那最坏的情况就是蛋的坚硬程度是楼高,那么每次都必须扔到楼高为止。

  有的人可能觉得这个不对,比如说那这样我从楼顶开始扔就好了啊?但是你从楼顶扔就不符合这道题的前提了,就是你的扔法必须能够确定这个蛋的坚硬程度,任何存在测不出来的可能的算法都不应该出现。

  然后呢?两个蛋的时候呢?

  一开始想到的是二分,但是又觉得2分并不能完全证明,于是作罢。无果,看题解。

  然后国内的题解五花八门,就没有一个能讲清楚的,尤其是你按这题来搜的时候。因为这题是鹰蛋问题的特化,那就更加无法理解了。

  然后上google,搜dp + egg,果然搜到了这个题目的英文,egg dropping puzzle.

  然后再搜这个题目,意外的发现youtube上有这类问题的讲解。

  https://www.youtube.com/watch?v=3hcaVyX00_4

  所以会英语对于程序员来说还是必须的啊。

  然后看懂了以后在 http://blog.csdn.net/snowy_smile/article/details/50386072 hdu大佬的网站里找到了特化的详细讲解。

  遂A题,详细以后再更新,毕竟我只会最初的那种,改一改再让我做我觉得还是做不出来。

————————————————————————————————————————————————————————

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<stack>
#include<list>
#include<set>
#define file_in freopen("input.txt","r",stdin)
#define MAX 100005
#define INF 0x3f3f3f3f
#define HASH 100019
using namespace std;
#define ll long long
#define FF(x,y) for(int i=x;i<y;i++)
ll dp[40][40];
int main() {
    for (int i = 1; i <= 32; i++)dp[i][1] = 1;//无论有多少蛋,操作只有一步的时候最大检测范围为2
    for (int j = 1; j <= 32; j++)dp[1][j] = j;//只有一个蛋,那只能一步步操作上去。
    for (int i = 2; i <= 32; i++)
    {
        for (int j = 2; j <= 32; j++)
        {
            dp[i][j] = dp[i - 1][j - 1] +1+ dp[i][j - 1];//蛋碎了,那么就操作次数-1,蛋数-1,同时向下找那个状态最多能测多少个,没碎,那么就向上找,操作次数-1.
        }
    }
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        int a, b;
        int flag = 0;
        scanf("%d %d", &a, &b);
        for (int i = 1; i <= 32; i++)
        {
            if (dp[b][i] >= a)
            {
                flag = 1;
                printf("%d\n", i);
                break;
            }
        }
        if (!flag)
        {
            printf("Impossible\n");
        }
    }


}
/*
7 4
3 1 2 3
2 1 4
3 5 6 7
1 6
*/

posted @ 2017-03-22 01:43  duskcloudxu  阅读(318)  评论(0编辑  收藏  举报