Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum

来源:互联网 发布:网络插口怎么接线 编辑:程序博客网 时间:2024/06/05 05:58
A. Little Pony and Expected Maximum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
6 1
output
3.500000000000
input
6 3
output
4.958333333333
input
2 2
output
1.750000000000
Note

Consider the third test example. If you've made two tosses:

  1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
  2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
  3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
  4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.

The probability of each outcome is 0.25, that is expectation equals to:

You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value







一个数学期望的题目,有dp的思想。但其实数学好的可以直接推公式。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    double i , n , m ;
    while(~scanf("%lf %lf", &m, &n))
    {
        double sum = 0.0 ;
        for(i=1;i<=m;i++)
        {
            sum+=i*(pow((i/m),n)-pow((i-1)/m,n));
        }
        printf("%lf\n", sum);
    }
    return 0;
}




这里说明一下公式的由来:

假设有6个面,投掷6次

4*(pow(4/6,6)-pow(3/6,6)):表示(面数不超过4的情况--不超过3的情况)*面数




总结:好好学习数学

0 0