E. Little Pony and Expected Maximum

Subject description:

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 m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability img. 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.

Examples

Input

Copy

6 1

Output

Copy

3.500000000000

Input

Copy

6 3

Output

Copy

4.958333333333

Input

Copy

2 2

Output

Copy

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:

img

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

Ideas:

The beginning of a sand sculpture. The maximum value of the title several times throwing look into the sum, which is the total face value obtained by throwing a few times to start the enumeration order, find the law, just when feeling a little prospect of a solution, not found, this title does not seem to, ( ⊙o⊙) ...

Title mean throwing a dice n times m of the surface, each have a maximum nominal value of throwing, seeking the maximum of all the possible throwing desired.

Start thinking:

A maximum of a case,

2, can be selected, once voted 2, the remaining cast is less than 2, twice voted 2, the remaining less than 2, ... max, n 2 plenary vote

A maximum of 3, etc., etc.

Finally, the formula is: \ (\ sum_ {i =. 1} ^ {m} \ sum_ {J =. 1} ^ {n-} i * C_n ^ J (i-. 1) ^ {NJ} \) , where i is the throwing the maximum face value, j is throwing in a few invested maximum.

A look at this formula, simplifying how ah? A look I do not know, look surprised:

\ (\ sum_ {j = 1 } ^ {n} C_n ^ j (i-1) ^ {nj} = C_n ^ 1 (i-1) ^ {n-1} + C_n ^ 2 (i-1) ^ {n-2} + ... + C_n ^ n (i-1) ^ 0 \) is a bit like binomial expansions ah.

What unfolded it?

Obviously one is \ ((i-1) \) , there is a 1, so think of \ ((i-1) the n-^ \) , but not ah, \ ((i-1) the n-^ ^ ^ n-C_n 0i = (-1) ^ n-1i + C_n ^ ^. 1 (-1) ^ {}. 1-n-+ ... + n-C_n ^ Ni ^ \) . Eh. . . Push does not go, and if directly apply the formula complexity directly \ (O (n ^ 2) \) , m and n are \ (10 ^ 5 \) order, one second is not enough.

Only to find another formula of.

Another idea: since each is the largest face value of i, n times that my vote does not exceed the number i is \ (i ^ n \) , each vote does not exceed the number of i-1 is \ ((i-1) ^ the n-\) , subtract the two to get what? I is the maximum number! There must also be the case for the denominations i, the maximum has been reduced because of all i-1 or less i.e. possible. Such \ (O (n) \) the result can be calculated. Note here that since the parameter is a function pow adjacent, a parameter can save it for the next iteration, so as to reduce the calculation amount. Also in order to prevent overflow, the final sum is divided to \ (m ^ n \) , each divided into \ (m ^ n \) and then summed.

\(\frac{pow(i,n)-pow(i-1,n)}{m^n}=\frac{pow(i/m,n)-pow((i-1)/m,n)}{1}\)

Yes incidentally also demonstrated: \ (\ sum_ {I =. 1} ^ {m} \ sum_ {J =. 1} ^ {n-} I * C_n ^ J (I-. 1) ^ {NJ} = \ sum_ {I m. 1} ^ {=} * I (I ^ N- (. 1-I) n-^) \) .

Code:

#include <iomanip>
using namespace std;
double n,m;
double ans = 0;
int main()
{
    cin >> m >> n;
    double tmp = 0;
    double last = 0;
    for(int i = 1;i<=m;i++)
    {
        tmp = pow(i/m,n);
        ans += (tmp-last)*i;
        last = tmp;
    }
    cout.setf(ios_base::fixed,ios_base::fixed);
    cout << setprecision(12) << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11286522.html