UVa11021

11021 Tribbles
GRAVITATION, n.
“The tendency of all bodies to approach one another with a strength
proportion to the quantity of matter they contain – the quantity of
matter they contain being ascertained by the strength of their tendency
to approach one another. This is a lovely and edifying illustration of
how science, having made A the proof of B, makes B the proof of A.”
Ambrose Bierce
You have a population of k Tribbles. This particular species of Tribbles live for exactly one day and
then die. Just before death, a single Tribble has the probability Pi of giving birth to i more Tribbles.
What is the probability that after m generations, every Tribble will be dead?
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line
containing n (1 n 1000), k (0 k 1000) and m (0 m 1000). The next n lines will give the
probabilities P0; P1; : : : ; Pn??1.
Output
For each test case, output one line containing ‘Case #x:’ followed by the answer, correct up to an
absolute or relative error of 10??6.
Sample Input
43
1 1
0.33
0.34
0.33
3 1 2
0.33
0.34
0.33
3 1 2
0.5
0.0
0.5
4 2 2
0.5
0.0
0.0
0.5
Universidad de Valladolid OJ: 11021 – Tribbles 2/2
Sample Output
Case #1: 0.3300000
Case #2: 0.4781370
Case #3: 0.6250000
Case #4: 0.3164062

题意:

有k只麻球,每只活一天就会死亡,临时前可能会产生一些新的麻球。产生i(0<=i<=n)个麻球的概率是Pi。给定m,求m天(或者不足m天)之后所有麻球都死亡的概率。

分析:

由于每只麻球的后代独立存活,只需要求出一开始只有1只麻球,m天会全部死亡的概率f(m)。由全概率公式:

f(i) = P0 + P1 * f(i - 1) + P2 * f(i - 1) ^ 2 + … + Pn * f(i - 1) ^ n。

最终答案为f(m) ^ k。

 1 #include <cstdio>
 2 #include <cmath>
 3 const int maxn = 1000;
 4 const int maxm = 1000;
 5 int n,k,m;
 6 double P[maxn + 1],f[maxn + 1];// f[i]表示麻球在i天后全死亡的概率
 7 int main(){
 8     int T; scanf("%d",&T);
 9     int kase = 0;
10     while(T--){
11         scanf("%d%d%d",&n,&k,&m);
12         for(int i = 0 ; i < n ; i++) scanf("%lf",&P[i]);
13         f[0] = 0,f[1] = P[0];
14         for(int i = 2 ; i <= m ; i++){
15             f[i] = 0;
16             for(int j = 0 ; j < n ; j++)
17                 f[i] += P[j] * pow(f[i - 1],j);
18         }
19         printf("Case #%d: %.7lf\n",++kase,pow(f[m],k));
20     }
21     return 0;
22 }

时间: 2024-08-24 09:38:45

UVa11021的相关文章

UVA11021 Tribles[离散概率 DP]

UVA - 11021 Tribles GRAVITATION, n. “The tendency of all bodies to approach one another with a strength proportion to the quantity of matter they contain – the quantity of matter they contain being ascertained by the strength of their tendency to app

UVA-11021 - Tribles(概率期望)

链接uva-11021 题意:开始有k只麻球,每只都是活一天就死,每只死前都会有pi的概率生出i只麻球.求m天后麻球死光的概率. 思路:各个麻球的死亡都是独立的,求对于一个麻球而言,m天后死光的概率就是f[m] 由全概率公式f[i] = p0 + p1 * f(i - 1) + p2 * f(i - 1)^2 + p3 * f(i - 1)^3....pn-1 * f(i - 1)^n-1 因为是用f[i-1]表示一只麻球i-1天后全部死亡的概率,j只后代全部死亡就是f[i-1]^j,就是j次方

【乱入】Uva11021麻球繁衍

就是根据概率公式入门算算. #include<bits/stdc++.h> const int N=1010; int n,m,k; double p[N],f[N]; int main(){ int T;scanf("%d",&T); for(int yql=1;yql<=T;yql++){ scanf("%d%d%d",&n,&k,&m); for(int i=1;i<=n;i++)scanf("

UVa11021 Tribles

概率 递推 每只麻球都是独立计算的. 可以递推,设f[i]表示一只麻球经过i天死光的概率,那么f[i]的k次方就是k只麻球经过i天死光的概率. 则f[i]=p[0]+p[1]*f[i-1]^1+p[2]*f[i-1]^2+...+p[n-1]*f[i-1]^(n-1) ↑直接死掉:生了一只,这一只在i-1天后死了:生了两只,这两只在i-1天后都死了...以此类推 1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm>

UVA11021 Tribbles

题意:K个麻球每个麻球生i个麻球概率为p[i]求所有麻球在第m天全部死亡的概率 题解:全概率公式f[i]代表一只麻球存活i天的概率,正向推 #include <bits/stdc++.h> #define maxn 10100 #define ll long long using namespace std; int n,k,m,T; double p[maxn], f[maxn]; int main(){ scanf("%d", &T); for(int i=1;

uva 11021 Tribles

https://vjudge.net/problem/UVA-11021 k只麻球,每只活一天就死亡,临死之前可能会生成0——n-1只麻球 给出 生成i只麻球的概率p, 问m天后所有麻球都死亡的概率 令dp[i]表示1只麻球产生的后代在前i天死亡的概率 定义 pj * dp[i-1]^j 表示1只麻球产生了j个后代,他们全在前i-1天死亡 dp[i]= Σ  p j*dp[i-1]^j ans=dp[m]^k #include<cmath> #include<cstdio> #de

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

概率dp专辑

uva11021 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1962 给定n,k,m 有k个麻雀,每只活一天就会死,临死之前生出i只麻雀的概率为pi ,  0<= i <n 问m天后,麻雀死光的概率 独立事件同时发生是每个事件的概率相乘, 每只麻雀都是独立的,只要求出一只麻雀m天后死亡的概率dp[m], 那么k只麻雀m天后死

●UVA 11021 tunnello

题链: https://vjudge.net/problem/UVA-11021题解: 概率DP. 定义dp[i]表示初始1只麻球的情况下,第i天都死完的概率. (因为每只麻球互相独立,那么最后答案为dp[i]^K.) 考虑dp[i]如何计算,仍然运用全概率公式: 把转移来源分为互相独立的部分,这里就是枚举第一天结束时,那只麻球生了几个仔仔. 如果生下了j个仔仔,那么问题就变成了相同子问题:即初始j个麻球,要在i-1后死完的概率为多少,显然为dp[i-1]^j 所以转移为:$$dp[i]=\su