[UVA]11021 Tribles

来源:互联网 发布:实矩阵的奇异值分解 编辑:程序博客网 时间:2024/06/06 05:03

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 rst 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; : : : ; Pn1.
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个麻球, 每个麻球过一天天都会死, 但是死的时候有几率生成 0 ~n-1只麻球, 概率分别为p0, p1…pn-1. 问m天内所有麻球死光的概率是多少.

题解:由于麻球之间都是相互独立的, 也就是若已知一只麻球m天内死光的概率, 那么只需要根据乘法原理, 将这个概率k次方即可. 我们设f[i]为一只麻球i天内死光的概率. 可以递推得到:
f[m] = p0 + f[i-1] * p1, f[i-1] ^2 * p2…f[i-1] ^(n-1) * pn-1;
因为p0的几率不产生则是p0的几率一天死掉, p1的几率产生一只, 那么就要求这只在剩下的i-1天内死掉, 那么就是f[i-1] * p1, 生成两只, 三只的…都是乘法原理(事件同时发生) , 用概率去乘f[i-1]的生成个数的次方.

话说vjudge第一为什么好像连概率都没有输入, 怎么A的, %…

#include<stdio.h>#include<cmath>const int maxn = 1005;int T, n, k, m, cas;double f[maxn], p[maxn], poww, ans;int main(){    scanf("%d", &T);    while(T--){        scanf("%d%d%d", &n, &k, &m);        for(int i = 1; i <= n; ++i) scanf("%lf", &p[i]);        f[1] = p[0];        for(int i = 1; i <= m; ++i){            f[i] = 0, poww = 1;            for(int j = 1; j <= n; ++j)                f[i] += p[j] * poww, poww *=  f[i-1];         }        ans = 1;        for(int i = 1; i <= k; ++i) ans *= f[m];        printf("Case #%d: %.7lf\n", ++cas, ans);    }}