ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LightOJ 1248 Dice (III)

2019-07-26 19:39:08  阅读:264  来源: 互联网

标签:const dice int 0.5 LightOJ cfrac 1248 III throw


Dice (III)

Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that means when you throw the dice, the probability of occurring any face is equal.

For example, for a fair two sided coin, the result is 3. Because when you first throw the coin, you will definitely see a new face. If you throw the coin again, the chance of getting the opposite side is 0.5, and the chance of getting the same side is 0.5. So, the result is

1 + (1 + 0.5 * (1 + 0.5 * …))

= 2 + 0.5 + 0.52 + 0.53 + …

= 2 + 1 = 3

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 105).

Output
For each case, print the case number and the expected number of times you have to throw the dice to see all its faces at least once. Errors less than 10-6 will be ignored.

Sample Input

5
1
2
3
6
100

Sample Output

Case 1: 1
Case 2: 3
Case 3: 5.5
Case 4: 14.7
Case 5: 518.7377517640

题意
n面的骰子,求每个面至少出现一次的投骰子次数的期望

题解
这看了半天也推不出来是个什么式子。
不看题解真想不起来几何分布了。
几何分布
X的概率分布P(X=k)=(1p)k1p  (k=12 )P(X=k)=(1-p)^{k-1}p\ \ (k=1、2\cdots)P(X=k)=(1−p)k−1p  (k=1、2⋯)
X表示在无限次伯努利试验中,事件A第一次发生的概率。则P(X)满足参数为p的几何分布
几何分布的期望为E(X)=1pE(X)=\cfrac{1}{p}E(X)=p1​
所以第一个面出现的概率nn\cfrac{n}{n}nn​
第二个面第一次出现的概率n1n\cfrac{n-1}{n}nn−1​
第i个面第一次出现的概率ni+1n\cfrac{n-i+1}{n}nn−i+1​
\cdots
故每个面至少出现一次的期望E(X)=i=1n1Pi=i=1nnni+1E(X)=\displaystyle\sum_{i=1}^{n}1·P_i=\displaystyle\sum_{i=1}^{n}\cfrac{n}{n-i+1}E(X)=i=1∑n​1⋅Pi​=i=1∑n​n−i+1n​

代码

#include <bits/stdc++.h>

using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const double eps = 1e-09;
const double PI = acos(-1.0);



int main(){
    int t,ca=1;cin>>t;
    while(t--){
        int n;scanf("%d",&n);
        double ans=0;
        for(int i = 1; i <= n; ++i){
            ans += 1.0*n/i;
        }
        printf("Case %d: %.10f\n",ca++,ans);
    }
    return 0;
}

标签:const,dice,int,0.5,LightOJ,cfrac,1248,III,throw
来源: https://blog.csdn.net/qq_41746268/article/details/97408285

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有