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 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
4
3 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
Sample Output
Case #1: 0.3300000
Case #2: 0.4781370
Case #3: 0.6250000
Case #4: 0.3164062

题意:给你 k个球,  一个球可以活一天,在它死的时候会有概率pi生出i个小球,(0<=i<n) 现在问你m天后 所有小球全部死亡的概率是多少

题解: 我们定义f[m] 为 一个小球 在活m天后死亡的概率 ,那么答案就是 f[m]^k

对于f[i] = P0 + P1 * (f[i-1]^1) + P2 * (f[i-1] ^ 2) + ............

递推得到答案

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N=; int main() {
int T, cas = , n, m, k;
double p[N],f[N];
scanf("%d",&T);
while(T--) {
scanf("%d%d%d",&n,&k,&m);
for(int i = ; i < n ; i++) scanf("%lf",&p[i]);
f[] = ;
for(int i = ; i <= m ; i++) {
f[i] = 0.0;
for(int j = ; j < n ; j++) {
f[i] += p[j] * pow(f[i-],j);
}
}
printf("Case #%d: %.7f\n",cas++, pow(f[m],k));
}
return ;
}

代码

上一篇:Unable to execute dex: Multiple dex files define异常的解决办法


下一篇:DAY14(PYTHONS)生成器进阶