UESTC 1051 Eggs broken【思维+期望Dp】

来源:互联网 发布:剑灵灵族捏脸数据图 编辑:程序博客网 时间:2024/05/21 04:39

Eggs broken

Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

Submit Status

There is a building which is nn floors high. Bob has KK same eggs now. It is known that, the egg will be broken if Bob throws it from the nthnth floor. Now Alice is wondering, what's the minimum expected times Bob will throw eggs until he finds the smallest xx, if Bob throws an egg from the xthxth floor the egg will be broken.

The xx is distributed in [1,n][1,n] uniformly.

As is known to everyone of you, Bob loves Alice very much. Could you tell Bob the answer to help Bob leave a good impression on Alice.

Input

The first line contains two integers nn and KK, which denote the number of floors and the number of eggs Bob has now.

It is guaranteed that 1≤n≤1000,1≤K≤151≤n≤1000,1≤K≤15

Output

Print the minimum expected times Bob will throw eggs in one line.

The answer should be rounded to 55 digits after the decimal point.

Sample input and output

Sample Input

Sample Output

4 2

2.00000

Source

The 13th UESTC Programming Contest Final

 

题目大意:


现在你要在一个楼高为N的楼房上找到最低能够摔碎鸡蛋的楼数,求测试的最小步数的期望。

已知在n层的时候一定可以摔碎。

现在你拥有K个鸡蛋,鸡蛋没有碎我们可以捡起来。


思路:


设定Dp【i】【j】表示i层楼高,有j个鸡蛋的最小测试步数期望。


那么我们不难推出其状态转移方程:


①dp【n】【k】=min(dp【n】【k】,dp【i】【k-1】*(i/n)+dp【n-i】【k】*(n-i)/n+1);

②dp【1】【k】=0;

③dp【n】【1】=(1+2+3+4+..........+n-1+n-1)【如果是x==1,需要操作1步,x==2,需要操作2步..........x==n-1,需要操作n-1步,而如果x==n,我们操作n-1步即可】;


时间复杂度O(n^2K);


Ac代码:

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;double dp[1005][25];double Cal(double a,double b){    return a/b;}void Slove(int a,int b){    memset(dp,0,sizeof(dp));    for(int i=1;i<=1002;i++)    {        for(int j=1;j<=22;j++)        {            dp[i][j]=100000000000000000;        }    }    for(int n=1;n<=a;n++)    {        for(int k=1;k<=b;k++)        {            if(n==1)dp[n][k]=0;            else            {                if(k==1)dp[n][k]=Cal((double)((n-1+((n)*(n-1))/2)),(double)(n));                else                {                    for(int i=1;i<=n;i++)                    {                        dp[n][k]=min(dp[n][k],Cal((double)dp[i][k-1]*i,(double)n)+Cal((double)dp[n-i][k]*(n-i),(double)n)+(double)1);                    }                }            }        }    }    printf("%.5lf\n",dp[a][b]);}int main(){    int n,k;    while(~scanf("%d%d",&n,&k))    {        memset(dp,0,sizeof(dp));        Slove(n,k);    }}











原创粉丝点击