• LightOJ


    题目链接:https://vjudge.net/problem/LightOJ-1248

    1248 - Dice (III)
    Time Limit: 1 second(s) Memory Limit: 32 MB

    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

    Output for Sample Input

    5

    1

    2

    3

    6

    100

    Case 1: 1

    Case 2: 3

    Case 3: 5.5

    Case 4: 14.7

    Case 5: 518.7377517640

    题意:

    问一个n面的骰子平均要抛多少次才能使得每个面都至少朝上一次?

    题解:

    1.设dp[k]为对于这个n面骰子,在出现了k面的情况下,还要抛多少次才能使得所有面都至少朝上一次。

    2.可知: dp[k] = k/n*dp[k] + (n-k)/n*dp[k+1] + 1。移项得:dp[k] = dp[k+1] + n/(n-k) 。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 1e5+100;
    18 
    19 double dp[MAXN];
    20 int main()
    21 {
    22     int T, n, kase = 0;
    23     scanf("%d", &T);
    24     while(T--)
    25     {
    26         scanf("%d", &n);
    27         dp[n] = 0;
    28         for(int i = n-1; i>=0; i--)
    29             dp[i] = dp[i+1]+1.0*n/(n-i);
    30         printf("Case %d: %.10lf
    ", ++kase, dp[0]);
    31     }
    32 }
    View Code
  • 相关阅读:
    文档注释
    配置环境变量
    Java编译-->运行
    DOS命令(日后补充)
    字符、字符串函数
    输入字符串,逆序输出
    求矩阵最大元素值,以及行号和列号
    Apache和Tomcat的区别
    C# params关键字
    JSP的隐式对象(session)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8444681.html
Copyright © 2020-2023  润新知