概率dp 例题

Discovering Gold

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 amount of gold.

Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have to find out the expected number of gold you can collect using the given procedure.

Input

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

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000.

Output

For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6 will be ignored.

Sample Input

3
1
101
2
10 3
3
3 6 9

Sample Output

Case 1: 101.0000000000
Case 2: 13.000
Case 3: 15

题目大意:
给n个格子,每个格子有ai的金子,初始位置为1,操作是扔六面骰子,扔几点走几步,每到达一个新位置,就得到该位置的金子。到n的时候停止,如果下次移动的位置超过n,重新投掷骰子直到合法位置。求所能得到金子数量的期望。

解题思路:
dp[i]表示从i位置开始的期望, 对于一个位置的i的期望,dp[i]=∑i+6j=i+1dp[j]6 , 当然后面不够6个的时候处理一下就好。

关于求期望,要从已知推到未知,就这个题来说,已知只能是必定到达最后一个格子。所以要从已知走向位置就是逆着求的。

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
#define MAX 1e9+7
//const int mm=1010;
typedef long long ll;
double dp[10000];
int main()
{
    ll T,i,j,n,k;
    scanf("%lld",&T);
    for(k=1;k<=T;k++)
    {
        scanf("%lld",&n);
        for(i=1;i<=n;i++)
            scanf("%lf",&dp[i]);
        ll m=1;
        for(i=n-1;i>=1;i--)
        {
            if(m>6)
                m=6;
            for(j=1;j<=m;j++)
            {
                dp[i]+=(double)dp[i+j]/m;
            }
            m++;
        }
        printf("Case %d: %lf
",k,dp[1]);
    }
    return 0;
}

Throwing Balls into the Baskets

You probably have played the game "Throwing Balls into the Basket". It is a simple game. You have to throw a ball into a basket from a certain distance. One day we (the AIUB ACMMER) were playing the game. But it was slightly different from the main game. In our game we were N people trying to throw balls into M identical Baskets. At each turn we all were selecting a basket and trying to throw a ball into it. After the game we saw exactly S balls were successful. Now you will be given the value of N and M. For each player probability of throwing a ball into any basket successfully is P. Assume that there are infinitely many balls and the probability of choosing a basket by any player is 1/M. If multiple people choose a common basket and throw their ball, you can assume that their balls will not conflict, and the probability remains same for getting inside a basket. You have to find the expected number of balls entered into the baskets after K turns.

Input

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

Each case starts with a line containing three integers N (1 ≤ N ≤ 16), M (1 ≤ M ≤ 100) and K (0 ≤ K ≤ 100) and a real number P (0 P ≤ 1). P contains at most three places after the decimal point.

Output

For each case, print the case number and the expected number of balls. Errors less than 10-6 will be ignored.

Sample Input

2
1 1 1 0.5
1 1 2 0.5

Sample Output

Case 1: 0.5
Case 2: 1.000000

思路:与篮子无关,最终结果都是要投进去,直接求

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
#define MAX 1e9+7
//const int mm=1010;
typedef long long ll;
double dp[10000],p,ans;
int main()
{
    ll T,i,j,n,k,m,l;
    scanf("%lld",&T);
    for(l=1;l<=T;l++)
    {
        scanf("%lld%lld%lld%lf",&n,&m,&k,&p);
        ans=(double)n*k*p;
        printf("Case %d: %lf
",l,ans);
    }
    return 0;
}

Island of Survival

You are in a reality show, and the show is way too real that they threw into an island. Only two kinds of animals are in the island, the tigers and the deer. Though unfortunate but the truth is that, each day exactly two animals meet each other. So, the outcomes are one of the following

a)      If you and a tiger meet, the tiger will surely kill you.

b)      If a tiger and a deer meet, the tiger will eat the deer.

c)      If two deer meet, nothing happens.

d)      If you meet a deer, you may or may not kill the deer (depends on you).

e)      If two tigers meet, they will fight each other till death. So, both will be killed.

If in some day you are sure that you will not be killed, you leave the island immediately and thus win the reality show. And you can assume that two animals in each day are chosen uniformly at random from the set of living creatures in the island (including you).

Now you want to find the expected probability of you winning the game. Since in outcome (d), you can make your own decision, you want to maximize the probability.

Input

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

Each case starts with a line containing two integers t (0 ≤ t ≤ 1000) and d (0 ≤ d ≤ 1000) where t denotes the number of tigers and d denotes the number of deer.

Output

For each case, print the case number and the expected probability. Errors less than 10-6 will be ignored.

4
0 0
1 7
2 0
0 10


Case 1: 1
Case 2: 0
Case 3: 0.3333333333
Case 4: 1

题目:有 t只老虎,d只鹿,还有一个人,每天都要有两个生物碰面,现在有以下规则:老虎不管碰到谁都吃掉,同类的话就同归于尽。问人存活下来的概率。

直接把鹿忽略掉,人存活下来的可能性只有一种就是到最后老虎自相残杀,如果老虎有奇数个那么人一定死,偶数个时候,总的碰面可能有C(t+1,1)种(从t+1个动物中选出一个直到最后也不碰面),人最后活着的可能有1(人坚持到最后)。所以概率就是1/(t+1).

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
#define MAX 1e9+7
//const int mm=1010;
typedef long long ll;
double dp[10000],p,ans;
int main()
{
    ll T,i,j,n,m,l;
    scanf("%lld",&T);
    for(l=1;l<=T;l++)
    {
        scanf("%lld%lld",&n,&m);
        if(n==0)
            ans=1;
        else
        if(n%2!=0)
           ans=0;
        else
            ans=(double)1/(n+1);
        printf("Case %d: %lf
",l,ans);
    }
    return 0;
}

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个点的色子, 掷一次色子获得每个点数的概率相同, 求得到所有点数时掷色子次数的期望值;

思路: 假设已经有k个点数已经出现过, 则掷下一次可能得到未出现过的点数, 期望为1, 也可能得到已出现过的点数, 概率为k / n, 若得到已出现的点数, 则继续掷色子, 所以期望

      E = 1 + k / n (1 + k / n (1 + n / k * (...)))

        = 1 + k / n + (k / n) ^ 2 + (k / n) ^ 3 + ...

        = 1 + k / (n - k)

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
#define MAX 1e9+7
//const int mm=1010;
typedef long long ll;
double dp[10000],p,ans;
int main()
{
    ll T,i,j,n,m,l;
    scanf("%lld",&T);
    for(l=1;l<=T;l++)
    {
        scanf("%lld",&n);
        ans=0;
        for(i=0;i<n;i++)
        {
            ans+=(double)1+(double)i/(n-i);
        }
        printf("Case %d: %lf
",l,ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zcy19990813/p/9702730.html