Problem A
Tribbles
Input: Standard Input
Output: Standard Output
GRAVITATION, n. |
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 mgenerations, 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 | Sample Output |
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 | Case #1: 0.3300000 Case #2: 0.4781370 Case #3: 0.6250000 Case #4: 0.3164062 |
k个虫子,每个虫子只能活一天。在虫子死时有pi的概率生出i个新虫子(0<=i<=n-1),求在m天后虫子全部死亡的概率。
(注意:虫子在m天之前死光也算)
题解:
独立+全概率公式.k个虫子死亡相互独立,所以考虑一个虫子:m天后全死光的概率f(m)
则有:f(i)=p(0)+p(1)*f(i-1)+p(2)*(f(i-1)^2)+...+p(n-1)*(f(n-1)^(n-1))
代码:
#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1
#define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0
#define sf scanf
#define pf printf
#define INF 1 << 29
#define eps 1e-6
const double PI = acos(-1.0);
#define lint __int64
#define LL long long
#define MAX 1e9 + 7
#define maxn 10005
//101^110=011 异或
#define ULLint unsigned long long //2^64-1>1.8*10^19
#define clr(x) memset(x, 0, sizeof(x))
#define Clr(x) memset(x, -1, sizeof(x))
#define TT 1006
using namespace std;
double d[TT], P[TT];
int m, n, k;
void init() {
memset(d, 0, sizeof(d));
memset(P, 0, sizeof(P));
}
void input() {
scanf("%d%d%d", &n, &k, &m);
for (int i=0; i<n; ++i){
scanf("%lf", &P[i]);
}
}
void solve() {
d[1] = P[0];
for (int i=2; i<=m; ++i){
d[i] = 0;
for (int j=0; j<n; ++j){
d[i] += P[j]*pow(d[i-1], (double)j);
}
}
}
void output(int i) {
double ans = 1;
for (int i=0; i<k; ++i){
ans *= d[m];
}
printf("Case #%d: %.8f\n", i, ans);
}
int main(void) {
int t, i;
while (~scanf("%d", &t)){
i = 0;
while (t --){
init();
input();
solve();
output(++i);
}
}
return 0;
}