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 for xcoins.
Help Roma determine the expected number of earned coins after the victory over n monsters.
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 100).
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.
1 3
1.0000000000
2 1
2.3333333333
10 2
15.9380768924
打怪升级之概率dp,窝不会啊,复杂度怎么够啊,内存也吃不消,还要精确到1e-9
所以直接省去了一些操作
官方推导,世界突然变得明朗起来
#include<bits/stdc++.h> using namespace std; double E[2][555]; int main() { int n,k; cin>>n>>k;int f1=0,f2=1; for(int i=n-1; i>=0; i--) { for(int j=1; j<550; j++) E[f1][j]=E[f2][j]*(j*1./(j+1)/k+(k-1)*1./k)+(j*1./2+j*1./(j+1))/k+1./(j+1)*E[f2][j+1]/k; swap(f1,f2); } printf("%.10f ",k*E[f2][1]); return 0; }