Tribbles,UVa 11021

       虽然网上及蓝书上都有解析和源代码,但我想理清其思路,故整理一遍!不对之处,请务必指正!

You have a population of k Tribbles. This particular species of Tribbles live for exactly one day andthen 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 linecontaining n (1 ≤ n ≤ 1000), k (0 ≤ k ≤ 1000) and m (0 ≤ m ≤ 1000). The next n lines will give theprobabilities P0, P1, . . . , Pn−1.

Output

For each test case, output one line containing ‘Case #x:’ followed by the answer, correct up to anabsolute 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

第一天全部死亡: (即未生出新的麻球)

    则f[1] = P0;(一天后死亡,且没有生出新的麻球)

第二天全部死亡:(则可能生出麻球 1,2,3,4....(n-1)  且 新出生的麻球都未生出新的麻球 )

    则f[2] = P1*(1只麻球1天后死亡 即 f[1]) + P2*(2只麻球都死亡 即 f[1]^2) +P3*(3只麻球都死亡 即 f[1]^3) +......+ Pn-1*(n-1只麻球1天都死亡 即 f[1]^(n-1) );

    即f[2] = P1*f[1] + P2*f[1]^2 + P3*f[1]^3 +......+P3*f[1]^(n-1);

第三天全部死亡:(指的是第二天新出生的麻球 同样可能生出麻球个数 1,2,3,4....(n-1)  且 新出生的麻球都未生出新的麻球)

    则f[3] = P1*(新出生的1只麻球将在2天后全部死亡 即 f[2])+ P2*(新出生的2只麻球将在2天后全部死亡 即 f[2]^2)+ P3*(新出生的3只麻球将在2天后全部死亡 即 f[2]^3) + ......+ Pn-1*(新出生的n-1只麻球将在2天后全部死亡 即 f[2]^(n-1));

    即f[3] = P1*f[2] + P2*f[2]^2 + P3*f[2]^3 +......+P3*f[2]^(n-1);

依此类推;

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 0x3f3f3f3f ;
int N,n,k,m;
double p[1005];
double f[1005];
int main(int argc, char** argv) 
{
scanf("%d",&N);
int t = 1;
while(t <= N )
{
memset(p,0,sizeof(p));
memset(f,0,sizeof(f));
scanf("%d%d%d",&n,&k,&m);
for(int i = 0; i < n; i++)
scanf("%lf",&p[i]);
f[0] = 0;
f[1] = p[0];
for(int i = 2; i <= m; i++)
{
for(int j = 0; j < n; j++)
f[i] += p[j]*pow(f[i-1],j); //pow()函数在math.h中查看
}
printf("Case #%d: %.7lf\n",t,pow(f[m],k));//由于最初有k个麻球,每个麻球死亡相互独立,所以最终答案 f(m)^k
t++;
}
return 0;
}


猜你喜欢

转载自blog.csdn.net/nothing_227/article/details/79855402