zoukankan      html  css  js  c++  java
  • SP1026 FAVDICE

    题目描述

    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?

    输入格式

    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.

    输出格式

    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.

    题意翻译

    一个n面的骰子,求期望掷几次能使得每一面都被掷到。

    输入格式

    2
    1
    12
    

    输出格式

    1.00
    37.24
    

    思路

    (dp_i)为表示当前已经得到了 ii 个面后的期望次数。那么我们会有(2)种情况

    (1:)掷出已经出现过的面,概率为(frac{i}{n})

    (2:)掷出没有出现过的面,概率为(frac{n-i}{n})

    则有:

    (dp_i=frac{i}{n}*dp_i+frac{n-i}{n}*dp_{i+1}+1)

    (frac{n-i}{n}*dp_i=frac{n-i}{n}*dp_{i+1}+1)

    (dp_i=dp_{i+1}+frac{n}{n-i})

    边界条件为(dp_n=0),倒推即可求出答案。

    代码

    /************************************************
    *Author        :  xzj213
    *Created Time  :  2020.01.30.17:44
    *Mail          :  xzj213@qq.com
    *Problem       :  SP1026
    ************************************************/
    #include <bits/stdc++.h>
    #define REP(i,a,b) for(register int i=(a);i<=(b);i++)
    #define DREP(i,a,b) for(register int i=(a);i>=(b);i--)
    #define mem(a,x) memset((a),(x),sizeof(a))
    #define pii pair<int,int>
    #define lson k<<1
    #define rson k<<1|1
    #define x first
    #define y second
    #define str(a) strlen(a)
    using namespace std;
    const int maxn=1000+5;
    int n,T;
    double dp[maxn];
    void chkmax(int &a,int b){if(a<b)a=b;}
    void chkmin(int &a,int b){if(a>b)a=b;}
    int read() {
        int x=0,f=1;
        char ch=getchar();
        while(ch>57 || ch<48){if(ch==45)f=-1;ch=getchar();}
        while(ch<=57 && ch>=48){x=x*10+ch-48;ch=getchar();}
        return x*f;
    }
    int main() {
        freopen("SP1026.in","r",stdin);
        freopen("SP1026.out","w",stdout);
        T=read();
    	while(T--) {
    		n=read();
    		dp[n]=0;
    		DREP (i,n-1,0) dp[i]=dp[i+1]+1.0*n/(n-i);
    		printf("%.2lf
    ",dp[0]);
    	}
        return 0;
    }
    
    
  • 相关阅读:
    MySQL中如何使用布尔类型【转】
    你所不知道的Android Studio调试技巧【转】
    设计模式之工厂模式(factory pattern)【转】
    layuiadmin+tp5后台内容管理系统【转】
    PHPStorm怎么修改选中的背景颜色呢?【转】
    PHP保留两位小数的几种方法【转】
    jquery的css()函数同时设置多个css属性值
    Flutter text设置行间距【转】
    Flutter入门-布局Container、Padding、Align、Center【转】
    redis下载地址
  • 原文地址:https://www.cnblogs.com/xzj213/p/12243283.html
Copyright © 2011-2022 走看看