Little Pony and Expected Maximum
2014-08-15 22:41
330 查看
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th
face contains mdots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her
to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
题目大意是一个m面的骰子,掷N次,求n次中最大值的期望。
那么根据数学,期望=每种情况发生概率*最大点数。对于每种情况的概率这里采用从反面计算的方法:如果掷出最大值为i的情况,那么情况数:
pow(i,n)-pow(i-1,n)【这里自己好好体会下】。概率即为情况数除以总情况数pow(m,n)。 化简后即为pow(i/m,n)-pow((i-1)/m,n)。程序中实现采用了更为简洁的表现方法。这样,将每次的计算结果记入ans,最后按题意输出就行了。
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th
face contains mdots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her
to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
题目大意是一个m面的骰子,掷N次,求n次中最大值的期望。
那么根据数学,期望=每种情况发生概率*最大点数。对于每种情况的概率这里采用从反面计算的方法:如果掷出最大值为i的情况,那么情况数:
pow(i,n)-pow(i-1,n)【这里自己好好体会下】。概率即为情况数除以总情况数pow(m,n)。 化简后即为pow(i/m,n)-pow((i-1)/m,n)。程序中实现采用了更为简洁的表现方法。这样,将每次的计算结果记入ans,最后按题意输出就行了。
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main(){ double n=0,m=0; cin >> m >> n; double t=0,l=0,ans=0; for (int i=1;i<=m;++i){ t=pow((i*1.0)/m,n); ans+=(t-l)*i; l=t; } printf("%.12f\n",ans); return 0; }
相关文章推荐
- codeforces C. Little Pony and Expected Maximum
- CF#259 dv2 CLittle Pony and Expected Maximum(二项式公式)
- A. Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 1)——Little Pony and Expected Maximum
- C. Little Pony and Expected Maximum
- Little Pony and Expected Maximum CodeForces - 453A (数学期望)
- 神奇stl- - Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 2) C. Little Pony and Expected Maximum
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
- Codeforces 453 A. Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
- A. Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum
- codeforces #259 DIV2 C题 Little Pony and Expected Maximum(容斥+快速幂+公式推导)
- [Codeforces453A]Little Pony and Expected Maximum(概率期望+快速幂)
- C - Little Pony and Expected Maximum
- Codeforces Round #259 (Div. 2) C. Little Pony and Expected Maximum
- CodeForces 453 A.Little Pony and Expected Maximum(快速幂)