湖南中医药大学2017年集训队第四场选拔赛-Problem B: Power Eggs
来源:互联网 发布:阿里巴巴网络推广 编辑:程序博客网 时间:2024/05/01 12:54
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(); }}
- 湖南中医药大学2017年集训队第四场选拔赛-Problem B: Power Eggs
- 湖南中医药大学2017年集训队第四场选拔赛-Problem D: Jug Hard
- 湖南中医药大学2017年集训队第四场选拔赛-Problem F: Virtual Friends
- 湖南中医药大学2017年集训队第二场选拔赛
- 湖南中医药大学2017年集训队第五场选拔赛
- 2017年集训队第四场选拔赛Trainsorting
- 2017年集训队第四场选拔赛 -string Game
- 中南大学2016年校队选拔赛第一场Problem E
- 中南大学2016年校队选拔赛第一场Problem A
- 中南大学2016年校队选拔赛第一场Problem D
- 中南大学2016年校队选拔赛第二场Problem F
- 中南大学2016年校队选拔赛第二场Problem A
- 2017南工软件ACM集训队选拔赛第一场
- 20130820 【南华大学 ACM】 个人选拔赛第二场 【B . SUME】
- 第四场选拔赛
- 湖南工业大学个人选拔赛第一场 题解
- 2016年湖南中医药大学考博英语考.试答.案《466068743+考過付》
- 2017年浙江中医药大学大学生程序设计竞赛-B:一生之敌(思维)
- 美滋滋,想想第四天了都激动,滋滋~~ 今天上js了(╯‵□′)╯︵┻━┻
- 《ImageNet Classification with Deep Convolutional Neural Networks》(AlexNet)论文翻译
- JSP技术基础概念
- Laravel基础-模板的引入方式
- 白话空间统计之二十五:空间权重矩阵(二)
- 湖南中医药大学2017年集训队第四场选拔赛-Problem B: Power Eggs
- 从一组字符串中取出数字
- C primer plus(编程练习)file-2.12-7
- 【HDU 5441】Travel
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- C primer plus(编程练习)file-2.12-8
- linux谷歌浏览器代理设置
- wx.navigateTo传递数组对象解决方法
- email正则表达式