D. World of Darkraft - 2

time limit per test 2 seconds

memory limit per test 256 megabytes

input standard input

output standard output

Roma found a new character in the game "World of Darkraft - 2". In this game the character fights monsters, finds the more and more advanced stuff that lets him fight stronger monsters.

The character can equip himself with k distinct types of items. Power of each item depends on its level (positive integer number). Initially the character has one 1-level item of each of the k types.

After the victory over the monster the character finds exactly one new randomly generated item. The generation process looks as follows. Firstly the type of the item is defined; each of the k types has the same probability. Then the level of the new item is defined. Let's assume that the level of player's item of the chosen type is equal to t at the moment. Level of the new item will be chosen uniformly among integers from segment [1; t + 1].

From the new item and the current player's item of the same type Roma chooses the best one (i.e. the one with greater level) and equips it (if both of them has the same level Roma choses any). The remaining item is sold for coins. Roma sells an item of level x of any type forx coins.

Help Roma determine the expected number of earned coins after the victory over n monsters.

Input

The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 100).

Output

Print a real number — expected number of earned coins after victory over n monsters. The answer is considered correct if its relative or absolute error doesn't exceed 10 - 9.

Examples

input

1 3

output

1.0000000000

input

2 1

output

2.3333333333

input

10 2

output

15.9380768924

题解:

这道题真的是好题啊……

我们不难发现,每一种武器的金币贡献都是相互独立的,因此我们只需要处理一种武器的情况,最后*k即可

那么我们考虑转移,设dp数组为f[i][j],表示带着等级为j的武器打i只怪,能得到的期望收益,

那么显然f[0][j]均为0,我们最终的目标就是求f[n][1]

接下来考虑转移,每一次打怪有1/k的概率掉落这种装备,有(k-1)/k概率不掉落,就无法从这种装备上得到收益

因此f[i][j]=(期望金币收益)/k+(k-1)*f[i-1][j]/k

而期望金币收益中,有1/(j+1)概率掉落高级装备,j/(j+1)概率掉落低级装备

因此 期望金币收益=(f[i-1][j+1]+j)/(j+1)+(f[i-1][j]+(j+1)/2)*j/(j+1)

其中(j+1)/2为低级装备的期望收益(平均数)

因此综上,f[i][j]=((f[i-1][j+1]+j)/(j+1.0)+j*(f[i-1][j]+(j+1.0)/2.0 )/(j+1.0))/(k*1.0)+(k-1)*f[i-1][j]/(k*1.0);

但是,这样是一个O(n2)的算法,时间超限,空间也可能超限,因此我们考虑优化

首先显然,f数组可以滚动,解决了空间问题

接着,我们发现1s内108=105*1000,因此我们第二次循环变成1000次以内就好了

那么,我们可不可以在n太大时把第二层n变成一个小数吗?这是正确的吗?

我们可以发现,如果j很大的话,装备升级的可能就很小

如果是f[i][i+1],总的概率是(1/k)i*1/(n+1)!,而题目中说“The answer is considered correct if its relative or absolute error doesn't exceed 10 - 9.”

因此这样小的数据我们完全可以扔掉不要

事实上,我们只要枚举j从1到1000左右即可,这样复杂度就被降到了1s以内,这样我们就解决了本题

代码见下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n,k;
double f[][];
int main()
{
scanf("%d%d",&n,&k);
int t=min(n,);
int now=;
for(int i=;i<=n;i++,now^=)
for(int j=t;j;j--)
f[now][j]=((f[now^][j+]+j)/(j+1.0)+j*(f[now^][j]+(j+1.0)/2.0 )/(j+1.0))/(k*1.0)+(k-)*f[now^][j]/(k*1.0);
printf("%.10lf",k*f[now^][]);
}

[codeforces464D]World of Darkraft - 2 概率期望的更多相关文章

  1. 【BZOJ-1419】Red is good 概率期望DP

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 660  Solved: 257[Submit][Status][Di ...

  2. uvalive 7331 Hovering Hornet 半平面交+概率期望

    题意:一个骰子在一个人正方形内,蜜蜂在任意一个位置可以出现,问看到点数的期望. 思路:半平面交+概率期望 #include<cstdio> #include<cstring> ...

  3. OI队内测试一【数论概率期望】

    版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] 测试分数:110 本应分数:160 改完分数:200 T1: 题 ...

  4. 2016 多校联赛7 Balls and Boxes(概率期望)

    Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...

  5. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  6. 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp

    题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...

  7. [LnOI2019]加特林轮盘赌(DP,概率期望)

    [LnOI2019]加特林轮盘赌(DP,概率期望) 题目链接 题解: 首先特判掉\(p=0/1\)的情况... 先考虑如果\(k=1\)怎么做到\(n^2\)的时间复杂度 设\(f[i]\)表示有\( ...

  8. 【loj6191】「美团 CodeM 复赛」配对游戏 概率期望dp

    题目描述 n次向一个栈中加入0或1中随机1个,如果一次加入0时栈顶元素为1,则将这两个元素弹栈.问最终栈中元素个数的期望是多少. 输入 一行一个正整数 n . 输出 一行一个实数,表示期望剩下的人数, ...

  9. bzoj 2969: 矩形粉刷 概率期望

    题目: 为了庆祝新的一年到来,小M决定要粉刷一个大木板.大木板实际上是一个W*H的方阵.小M得到了一个神奇的工具,这个工具只需要指定方阵中两个格子,就可以把这两格子为对角的,平行于木板边界的一个子矩形 ...

随机推荐

  1. day9 匿名函数 lambda

    1. list列表排序 #### sort排序 nums = [,,,,,] nums.sort() print(nums) ### 结果 [, , , , , ] ######## 逆序 In [] ...

  2. Scratch3.0设计的插件系统(上篇)

    我们每个人在内心深处都怀有一个梦想: 希望创造出一个鲜活的世界,一个宇宙.处在我们生活的中间.被训练为架构师的那些人,拥有这样的渴望: 在某一天,在某一个地方,因为某种原因,创造出了一个不可思议的.美 ...

  3. iOS 关于权限设置的问题

      在info.plist文件下添加 <key>NSContactsUsageDescription</key>    <string>请求访问通讯录</st ...

  4. 如何运用 Powershell 修改Office365和AD账户

    这段时间需要大量地修改AD用户的一些属性,例如邮件,UPN,登录名等等,以便和Office365的登录账号保持一致.写了个简单脚本进行批量修改. #Import AD ModuleImport-Mod ...

  5. 《图解 HTTP 》阅读 —— 第三章

    第3章 HTTP 报文内的 HTTP 信息 用于 HTTP 协议交互的信息称为 HTTP 报文:请求报文和响应报文 HTTP 在传输数据时通过编码可以提升速率,能有效的处理大量数据,但是会消耗更多的C ...

  6. thinkphp5框架生成二维码

    二话不说,先上代码: 第一中: 不用再本地保存文件,直接在前台页面显示: 这是控制器里面的内容,哦,对啦,首先要下载SDK:.phpqrcode类文件下载,下载地址:https://sourcefor ...

  7. 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴

    上一节学习了使用Cocos2d-x播放音乐的方法,但是那种方法一般只适合于播放较大的音乐,而一般比较短小的音乐(如游戏中的打斗.按键音效等)则要通过playEffect来播放.本节使用该方法以及之前学 ...

  8. scikit-learn使用PCA降维小结

    本文在主成分分析(PCA)原理总结和用scikit-learn学习主成分分析(PCA)的内容基础上做了一些笔记和补充,强调了我认为重要的部分,其中一些细节不再赘述. Jupiter notebook版 ...

  9. Python学习之web框架 Flask

    一.通过PIP 安装Flask 1.1 Windows环境安装pip A.首先PIP进入官网(https://pypi.python.org/pypi/pip)下载gz包 B.对gz压缩包进行解压,解 ...

  10. leetcode个人题解——#49 Group Anograms

    思路:利用c++ stl的map来实现关键字匹配, 遍历strs容器类,对其中每一个string进行按字典序排序后,查找是否存在这样一个键,如不存在,存储该键,并将str[i]作为键映射的第一个元素: ...