概率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;
}
/*

*/

 

目录
相关文章
|
11月前
UVa10123 No Tipping
UVa10123 No Tipping
43 0
|
11月前
UVa11776 - Oh Your Royal Greediness!
UVa11776 - Oh Your Royal Greediness!
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...
1547 0
|
C++
UVA 之10010 - Where's Waldorf?
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/24863879 ...
694 0
|
存储 固态存储
|
人工智能
uva 10189 Minesweeper
/* Minesweeper WA了n次才知道uva格式错了也返回wa没有pe啊尼玛 */ #include&lt;iostream&gt; #include&lt;stdio.h&gt; #include&lt;string.h&gt; using namespace std; char a[105][105]; int main() { int i,j,n,m,
919 0
uva 10730 - Antiarithmetic?
点击打开链接uva 10730 思路:枚举等差中项 分析: 1 给定一个n个数的序列判断是否有等差子序列 2 很明显我们如果要判断是否有等差子序列的话,只要去判断是否有长度为3的等差子序列 3 对于n
821 0
uva10859Placing Lampposts
题意:给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮,每盏灯将照亮以他为一个端点的所有边,在灯的总数最小的前提下,被两盏灯同时照亮的边数应当尽量大。 分析:d(i,j)表示i的父节点放灯的状态为j(1表示放,0不放),以i为根的树的最小x值     x=Ma+c, a表...
770 0
|
BI 人工智能
UVA11292
题意:有n个恶龙,有m个骑士可雇佣,每个骑士能力为x,表示可以砍掉恶龙的不超过x的头,且雇佣他需要x金币。要求砍掉恶龙所有的头且付金币最少。类型:排序+模拟代码: #include #include #include using namespace std; const int m...
707 0