SPOJ Favorite Dice(数学期望)

BuggyD loves to carry his favorite die around. Perhaps you wonder why it‘s his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:

What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?

Input

The first line of the input contains an integer t, the number of test cases. t test cases follow.

Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD‘s die.

Output

For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.

Example

Input:
2
1
12

Output:
1.00
37.24

题意:

甩一个n面的骰子,问每一面都被甩到的次数期望是多少。

思路:

比较简单,公式:初始化dp[]=0;  dp[i]=i/n*dp[i]+(n-i)/n*dp[i+1]+1;  化简逆推即可。  求的是dp[0];

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
double dp[2000];
int main()
{
    int T,i,j,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n); dp[n]=0;
        for(i=n-1;i>=0;i--) dp[i]=(dp[i+1]*(n-i)/n+1)*n/(n-i);
        printf("%.2lf\n",dp[0]);
    } return 0;
}
时间: 2024-10-02 21:36:09

SPOJ Favorite Dice(数学期望)的相关文章

SPOJ FAVDICE 数学期望

题目大意: 一个有n面的色子抛掷多少次能使所有面都能被抛到过,求期望值 总面数为n,当已经抛到过 i 个不同面时,我们抛出下一个不同面的概率为 (n-i)/n,那么抛的次数为 n/(n-i) 将所有抛出下个面的次数累加起来就好了 1 #include <cstdio> 2 int main(){ 3 int kase,n; 4 scanf("%d",&kase); 5 while(kase--){ 6 scanf("%d",&n); 7

HDUBoard Game Dice (数学期望)

推出的公式是M^x*x/N,大概意思是M^x*x这些种可能后一定会找出一个裁判,在除以N为数学期望. 可能和数学公式还有关系. #include<stdio.h> #include<math.h> __int64 gcd(__int64 a,__int64 b){ return b == 0 ? a : gcd(b, a%b); } int main() { __int64 i,j,n,m,t,x,a,b,temp,sum; scanf("%I64d",&

ZOJ3329-One Person Game(概率DP求数学期望)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namelyDie1, Die2 and Die3. Die1 hasK1 faces. Die2 has K2 faces.Die3 has K3 faces. All the dic

LightOJ 1030 数学期望

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1030 uDebug Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amoun

数学期望

数学期望又称均值(加权均值),例如 甲8环,9环,10环的概率分别为0.1,0.8,0.1,即权重,则加权均值为8*0.1+9*0.8+10*0.1=9:同理乙的加权均值为8.95 则甲的平均成绩优于乙 对于离散型随机变量 连续型随机变量

数学1——概率与数学期望

1.什么是数学期望? 数学期望亦称期望.期望值等.在概率论和统计学中,一个离散型随机变量的期望值是试验中每一次可能出现的结果的概率乘以其结果的总和. 这是什么意思呢?假如我们来玩一个游戏,一共52张牌,其中有4个A.我们1元钱赌一把,如果你抽中了A,那么我给你10元钱,否则你的1元钱就输给我了.在这个游戏中,抽中的概率是$\frac{1}{13} ( \frac{4}{52} ) $,结果是赢10元钱:抽不中概率是$\frac{12}{13}$,结果是亏1元钱.那么你赢的概率,也就是期望值是$-

poj3682:数学期望,O(1)做法附推导过程

这几天一直在磨蹭这题..第一个答案很容易,但在第二个答案我无法算出来了,于是只好求助于Zayin.Zayin又求助于我们年级里面的一个研究生数学老师..而现在终于算出来了,我看了看,自己也推出来几次了,先看题:) King Arthur's Birthday Celebration Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2921 Accepted: 926 Description King Arthur is an

HDU 4405 飞行棋上的数学期望

突然发现每次出现有关数学期望的题目都不会做,就只能找些虽然水但自己还是做不出的算数学期望的水题练练手了 题目大意: 从起点0点开始到达点n,通过每次掷色子前进,可扔出1,2,3,4,5,6这6种情况,扔到几前进几,当然对应飞行通道可以通过x直达一点y,x<y,计算到达n点或超过n 点要扔色子的次数的数学期望 从某一点 i 扔完色子可到达 i+1,i+2,i+3,i+4,i+5,i+6这6个点,令dp[i]为到达末尾的数学期望 那么到达之后6个点的数学期望是一样的,那么dp[i]=dp[i+1]*

概率论,简要数学期望(转载)

概率论(https://ruanx.pw/post/%E6%A6%82%E7%8E%87%E8%AE%BA.html) 这东西并不难学.这片博客主要介绍离散概率.连续概率.期望与微积分…… 离散型概率入门 计算方法 首先,我们来讨论一个最原始的问题:抛一个质地均匀的硬币,抛中正面的几率是多大?显然50%50%. 那么问题加深一番:抛两个质地均匀的硬币,都抛中正面的几率是多大?显然25%25%. 进一步,抛nn个硬币,全都正面朝上的几率是0.5×0.5×0.5?=0.5n0.5×0.5×0.5?=

HDU3910(数学期望题,题目难懂)

Liang Guo Sha Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 590    Accepted Submission(s): 426 Problem Description Maybe you know “San Guo Sha”, but I guess you didn’t hear the game: “Liang Gu