CodeForces 454C——数学——Little Pony and Expected Maximum

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 . 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 Input

Input

6 1

Output

3.500000000000

Input

6 3

Output

4.958333333333

Input

2 2

Output

1.750000000000

Hint

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

/*
   对于一个数i他出现的次数为i^m - (i-1)^m
   (1^m - 0^m)*1 + ... + (i^m - (i-1)^m) * i + .... + (n^m - (n-1)^m)*m
   化简得 n^(m+1) - (1^m + 2^m + 3^m + ... + (n-1)^m)
   除以总数 n^m 得
   n - (1/n)^m - (2/n)^m - (3/n)^m - ... - (n-1/n)^m
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)){
    double x = (double)n;
    for(int i = 1; i < n; i++){
       x -= pow(1.0*i/n, m);
    }
    printf("%.10f\n", x);
}
return 0;
}

  

时间: 2024-10-03 04:00:40

CodeForces 454C——数学——Little Pony and Expected Maximum的相关文章

Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)

题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是所有最大值是xi的情况数/总情况数一共是m^n种,掷n次,所有最大值是xi的情况数应该是xi^n,但是这里边却包含着最大值非xi且不超过xi的种数,所以再减去最大值是xi-1或者最大值不超过这个的情况数.即sum += xi * (xi^n-(xi-1)^n)/m^n,但是这样求肯定是不行,因为m

Codeforces 454C - Little Pony and Expected Maximum

454C - Little Pony and Expected Maximum 思路: m面的骰子掷n次,总共有m^n种情况,如果一种情况的最大值是m,那么它肯定包含m,那我们在所有情况下挖掉不包含m的情况:(m-1)^n,所以最大值是m的情况数是m^n-(m-1)^n,同理可得最大值是m-1的情况数是(m-1)^n-(m-2)^n.... 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define

Codeforces 453A Little Pony and Expected Maximum 概率题Orz

题目链接:点击打开链接 #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; int main() { int i, j; double m,n; while(cin>>m>>

codeforces——Little Pony and Expected Maximum

1 /* 2 我们枚举每次选择最大数值的情况:m个数, 投掷n次 3 最大值是1: 1种 4 2: 2^n-1 5 3: 3^n-2^n 6 ..... 7 m: m^n-(m-1)^n 8 9 所以最后的结果=sum((k/m)^n - ((k-1)/m)^n) (1<=k<=m) 10 不要这样求(k^n/m^n)数据可能会很大! 11 */ 12 #include<iostream> 13 #include<cstdio> 14 #include<cmath

Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum

题目链接 题意: 分析: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio> 6 #include <vector> 7 #include <algorithm> 8 #define LL long long 9 using namespace std; 10 11 in

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

题目连接 题意: 输入n.m,表示一个n面的色子(面上的值为1-n),投掷m次,求得到的最大值的期望(1?≤?m,?n?≤?105). 分析: 假设当前得到的最大值是Max,那么对应的概率是:sigma(C(m,k) * ((1 / n) ^ k )*(((Max - 1) / n) ^ (m - k)) ),(1 <= k <= n):化简就可以得到:sigma(C(m,k) * ((1 / n) ^ k )*(((Max - 1) / n) ^ (m - k)) ) - ((Max - 1

Little Pony and Expected Maximum

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 firs

CodeForces 453A Little Pony and Expected Maximum

题意: n面骰子掷m次,求最大值的期望. 别人的做法:最大值为i的概率=所有点数<=i的概率-所有点数<=i-1的概率,然后直接算. sb做法:精度要求1e-4,而m较大时最大值是一个较小数的概率非常小,精度范围内不影响答案,所以直接dp,f[i][j]表示掷了i次之后最大值为j的概率,通过前缀和优化做到O(n)转移 方程为f[i][j]=(f[i-1][1]+f[i-1][2]+f[i-1][3]+-.+f[i][j-1])*(1/n)+f[i-1][j]*(j/n) 因为点数越小概率越小所

codeforces C. Little Pony and Expected Maximum

题意:一个筛子有m个面,然后扔n次,求最大值的期望; 思路:最大值为1 有1种,2有2n-1种,  3有3n -2n 种   所以为m的时有mn -(m-1)n 种,所以分别求每一种的概率,然后乘以这个值求和就可以. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 using nam