LightOJ1248---Dice (III)(概率dp)

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

Problem Setter: Jane Alam Jan

水题概率dp

/*************************************************************************
    > File Name: g.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月30日 星期四 16时17分00秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

double dp[100100];
int n;

double dfs(int cur) {
    if (dp[cur] != -1.0) {
        return dp[cur];
    }
    double p = (n * 1.0 / (n - cur));
    dp[cur] = dfs(cur + 1) + p;
    return dp[cur];
}

int main() {
    int icase = 1, t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 0; i <= n; ++i) {
            dp[i] = -1.0;
        }
        dp[n] = 0;
        printf("Case %d: %.12f\n", icase++, dfs(0));
    }
}
时间: 2024-08-25 10:34:58

LightOJ1248---Dice (III)(概率dp)的相关文章

Dice (III) 概率dp

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; int t,n; double dp[100010]; int main() { scanf("%d",&t); int cas=1; while(t--) { scanf("%d",&n); dp[n]=0

Light OJ 1248 - Dice (III) 概率DP

n个面的骰子 求每个面至少扔到一次的期望值 设dp[i]为已经扔了i个不同面的期望值 dp[n] = 0 求dp[0] 因为dp[i]为还需要扔i个不同的面 每次可能扔中已经扔过的面或者没有扔到过的面2中情况 所以dp[i] = (i/n)*dp[i] + (n-i)/n*dp[i+1] +1 等号2边都有dp[i] 移项得dp[i] = dp[i+1]+n/(n-i) #include <cstdio> #include <cstring> #define imax 100005

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0

HihoCoder1339 Dice Possibility(概率DP+母函数)

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and the sum of the numbers equals to M? 输入 Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600) 输出 Output the possibility in percentage with 2 decimal places. 样例输入 2 10 样例输出 8.3

HihoCoder - 1339 Dice Possibility(概率dp)

题意:求用N(1<=N<=100)个骰子掷出M(1<=M<=600)的概率 分析:直接求概率可能出现6^100次方,会爆精度.可以用一个数组dp[i][j]记录用i个骰子掷出j的概率.i为0时无论j是多少,概率都是0.i为1时,j从1-6的概率都是1/6.其余可以递推得到 dp[i][j]  = 0 (j<i || j>6*i),sigma(dp[i-1][max(0,j-k)])  (1<=k<=6) #include<stdio.h> #in

HDU 4652 Dice(概率dp)

Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0 m n:

LightOJ - 1248 Dice (III) 期望 + dp

题目大意:给出一个n面的色子,问看到每个面的投掷次数期望是多少 解题思路:水题啊,竟然被卡了那么久,也是醉了,给题目所给的那个样例误导了...不怪那个 怪自己太弱了,还得继续训练啊... 设dp[i]表示扔到i个不同面的期望,那么 dp[i + 1] = i / n * dp[i + 1] + (n - i) / n * dp[i] + 1 整理得 dp[i + 1] = n / (n - i) + dp[i] + 1 #include<cstdio> #define maxn 100010

Throwing Dice(概率dp)

C - Throwing Dice Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu LightOJ 1064 uDebug Description n common cubic dice are thrown. What is the probability that the sum of all thrown dice is at least x? Input Input starts wit

HDU 4599 Dice (概率DP+数学+快速幂)

题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n),这个用DP来推公式,d[i],表示抛 i 次连续的点数还要抛多少次才能完成.那么状态转移方程就是 d[i] = 1/6*(1+d[i+1]) + 5/6*(1+d[1]), 意思就是说在第 i 次抛和上次相同的概率是1/6,然后加上上次抛的和这一次,再加上和上次不同的,并且又得从第1次开始计算. 边界就是