题目描述:

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

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

思路:

刚开始沙雕了。把题目中的几次投掷的最大值看成了总和,也就是投掷几次的得到的总面值,开始枚举排列,找规律,就在感觉有点眉目时,发现不对,题目好像不是这个,(⊙o⊙)…

题目的意思是投掷一枚m面的色子n次,每次投掷的面值有个最大值,求所有可能的投掷的最大期望。

开始的思路:

最大为一,一种情况,

最大为2,可以选择,有一次投了2,剩下投了少于2,两次投了2,剩下少于2,...,n次全投2

最大为3,等等等等

最后公式是:\(\sum_{i=1}^{m}\sum_{j=1}^{n}i*C_n^j(i-1)^{n-j}\),其中i是投掷的最大面值,j是投掷中几次投到最大值。

一看,这个公式,怎么化简啊?一看不知道,再看吓一跳:

\(\sum_{j=1}^{n}C_n^j(i-1)^{n-j}=C_n^1(i-1)^{n-1}+C_n^2(i-1)^{n-2}+...+C_n^n(i-1)^0\)是不是有点像二项式的展开式啊。

是什么的展开呢?

很显然其中一项是\((i-1)\),还有一项是1,所以想到了\((i-1)^n\),但不对啊,\((i-1)^n=C_n^0i^n(-1)^n+C_n^1i^1(-1)^{n-1}+...+C_n^ni^n\)。诶。。。推不走了,要是直接套用公式复杂度直接\(O(n^2)\),m和n都是\(10^5\)量级的,一秒根本不够。

只有另找公式了。

换个思路:既然是每次最大面值为i,那我投n次不超过i的数目是\(i^n\),每次投不超过i-1的数目是\((i-1)^n\),两者相减得到什么?就是最大为i的数目!还是必有为面值是i的情况,因为已经减了最大是i-1即以下的所有可能了。这样\(O(n)\)内可以算出结果。这里注意的是用pow函数因为参数是相邻的,可以把一个参数存起来供下一次迭代使用,从而减少计算量。还为了不让溢出,最后要总和除以\(m^n\),变成每次除以\(m^n\)再求和。

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

对了顺带还证明了:\(\sum_{i=1}^{m}\sum_{j=1}^{n}i*C_n^j(i-1)^{n-j}=\sum_{i=1}^{m}i*(i^n-(i-1)^n)\)。

代码:

#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;
}

E. Little Pony and Expected Maximum(组合期望)的更多相关文章

  1. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  2. 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)的是 ...

  3. CF453A Little Pony and Expected Maximum 期望dp

    LINK:Little Pony and Expected Maximum 容易设出状态f[i][j]表示前i次最大值为j的概率. 转移很显然 不过复杂度很高. 考虑优化.考虑直接求出最大值为j的概率 ...

  4. Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题

    A. Little Pony and Expected Maximum Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  5. 嘴巴题9 Codeforces 453A. Little Pony and Expected Maximum

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  6. CodeForces 454C Little Pony and Expected Maximum

    Little Pony and Expected Maximum Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I6 ...

  7. A. Little Pony and Expected Maximum

    Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she ...

  8. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

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

    题目链接 题意:一个m个面的骰子,抛掷n次,求这n次里最大值的期望是多少.(看样例就知道) 分析: m个面抛n次的总的情况是m^n, 开始m==1时,只有一种 现在增加m = 2,  则这些情况是新增 ...

随机推荐

  1. Serializable笔记

    什么是序列化与反序列化? 把对象转换为字节序列的过程称为对象的序列化.把字节序列恢复为对象的过程称为对象的反序列化. 序列化的用途? 简单来说,是为了方便存储与传输. 存储:在很多应用中,需要对某些对 ...

  2. lnmp+discuz使用redis缓存(待进一步研究)

    一直说Redis.Redis缓存.一直不清楚怎么用.于是花点时间研究了一下,但是还没搞懂.先把大概内容记录一下,待后续继续学习 1.首先部署lnmp环境,这个我的博客有些,请自行搜索 2.给php添加 ...

  3. [Go] 数据类型,变量与变量作用域,常量

    // var.gopackage main import ( "fmt" ) func main() { // 声明变量的一般形式是使用 var 关键字,可以一次声明多个变量 // ...

  4. Sqlserver (转载)事物与锁

    1   概述 本篇文章简要对事物与锁的分析比较详细,因此就转载了. 2   具体内容 并发可以定义为多个进程同时访问或修改共享数据的能力.处于活动状态而互不干涉的并发用户进程的数量越多,数据库系统的并 ...

  5. DRF框架(八)——drf-jwt手动签发与校验、搜索过滤组件、排序过滤组件、基础分页组件

    自定义drf-jwt手动签发和校验 签发token源码入口 前提:给一个局部禁用了所有 认证与权限 的视图类发送用户信息得到token,其实就是登录接口,不然进不了登录页面 获取提交的username ...

  6. go语言浅析二叉树

    Hello,各位小伙伴大家好,我是小栈君,今天给大家带来的分享是关于关于二叉树相关的知识点,并用go语言实现一个二叉树和对二叉树进行遍历. 我们主要针对二叉树的概念,go实战实现二叉树的前序遍历.中序 ...

  7. Postman中添加真实请求(Chrome Networks中的全部请求,含https)copy as har

    Postman中添加真实请求(Chrome Networks中的全部请求,含https) xyxzfj 关注 2018.05.22 19:44* 字数 559 阅读 1176评论 0喜欢 0 Post ...

  8. Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)

    day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库:    简称:DataBase ---->DB    数据库即存放数据的仓库, ...

  9. ES6数组方法总结

    关于数组中forEach() .map().filter().reduce().some().every()的总结 1. forEach() let array = [1,2,3,4]; array. ...

  10. Integer装箱拆箱、参数传递

    拆箱装箱 举个例子 @Test public void testEquals() { int int1 = 12; int int2 = 12; Integer integer1 = new Inte ...