概率dp - UVA 11021 Tribles

简介: Tribles  Problem's Link:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059   Mean:  有k个细菌,每个细菌只能存活一天,在死去之前可能会分裂出0,1,2....n-1个细菌,对应的概率为p0,p1,p2....pn-1。

Tribles 

Problem's Link:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059


 

Mean: 

有k个细菌,每个细菌只能存活一天,在死去之前可能会分裂出0,1,2....n-1个细菌,对应的概率为p0,p1,p2....pn-1。

问:所有细菌在第m天全部灭亡的概率是多少?(m天以前灭亡也算在内)

analyse:

由于每一个细菌的生存是独立的,所以我们可以先算出一个细菌的概率为PP,最终答案应是:PP^k。

设dp[i]表示第i天全部灭亡的概率,那么:

  dp[i] = p0*(dp[i-1]^0) + p1*(dp[i-1]^1) + p2*(dp[i-1]^2) + ...pn-1*(dp[i-1]^(n-1))

其中pi*(dp[j-1]^i)表示:该细菌分裂成了i个,这i个细菌在第j-1天灭亡的概率。

由于每个细菌独立,所以是乘法,也就是i次方。

对于dp[0],代表第0天就全部灭亡,也就是根本没有分裂,所以dp[0]=p0.

 

Time complexity: O(N)

 

Source code:

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-26-20.36
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

const int MAXN = 1010;
double p [ MAXN ], dp [ MAXN ];
int main()
{
      ios_base :: sync_with_stdio( false);
      cin . tie( 0);
      int t;
      scanf( "%d" , & t);
      for( int Cas = 1; Cas <= t; ++ Cas)
      {
            int n , k , m;
            scanf( "%d %d %d" , &n , & k , & m);
            for( int i = 0; i <n; ++ i)
                  scanf( "%lf" , &p [ i ]);
            dp [ 0 ] =p [ 0 ];
            for( int i = 1; i < m; ++ i)
            {
                  dp [ i ] = 0.;
                  for( int j = 0; j <n; ++ j)
                  {
                        dp [ i ] +=p [ j ] * pow( dp [ i - 1 ], j);
                  }
            }
            printf( "Case #%d: %.7f \n " , Cas , pow( dp [ m - 1 ], k));

      }
      return 0;
}
/*

*/

 

目录
相关文章
|
8月前
uva10038 Jolly Jumpers
uva10038 Jolly Jumpers
23 0
|
8月前
UVa10123 No Tipping
UVa10123 No Tipping
38 0
|
8月前
UVa11776 - Oh Your Royal Greediness!
UVa11776 - Oh Your Royal Greediness!
36 0
|
8月前
uva10152 ShellSort
uva10152 ShellSort
40 0
|
算法
UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494...
1535 0
|
机器学习/深度学习
uva 12470 Tribonacci
点击打开uva12470  思路: 矩阵快速幂 分析: 1 裸题 代码: /************************************************ * By: chenguolin ...
963 0
uva 10273 Eat or Not to Eat?
点击打开链接uva 10273 思路: 暴力求解 分析: 1 题目要求没有吃掉的奶牛的个数已经最后一次吃掉奶牛的天数 2 没有其它的方法只能暴力,对于n头牛的n个周期求最小公倍数,然后在2个公倍数之内暴力求解 代码: #inclu...
788 0
uva 1203 Argus
点击打开链接uva 1203 思路: 优先队列 分析: 1 题目要求前k个事件的编号,我们利用优先队列来维护即可 2 优先队列保存的是一个struct,因此我们需要重载 s.
1278 0
uva 10730 - Antiarithmetic?
点击打开链接uva 10730 思路:枚举等差中项 分析: 1 给定一个n个数的序列判断是否有等差子序列 2 很明显我们如果要判断是否有等差子序列的话,只要去判断是否有长度为3的等差子序列 3 对于n
814 0