Problem A
Tribbles
Input: Standard Input
Output: Standard Output
GRAVITATION, n. |
Ambrose Bierce
You have a population of kTribbles. 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, everyTribble 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只麻球,每只只活一天,临死前会产仔,产i只小麻球的 概率为pi,问m天后所有麻球全部死亡的概率;
思路:因为每只麻球都是相互独立的,所以只需求刚开始只有一只麻球,m天后其后代全部死亡的概率f[m],然后k只麻球最后全部死亡的概率就是 pow(f[m],k);
对于一只麻球,m天全死亡包含第一天、第二天、、、、、第m天死亡事件,因此一只麻球第i天死亡的概率f[i] = p0 + p1*f[i-1] + p2*f[i-2]^2+.......+ pn-1*f[i-1]^(n-1);
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 int main() 5 { 6 int test; 7 scanf("%d",&test); 8 for(int item = 1; item <= test; item++) 9 { 10 int n,k,m; 11 double p[1100],f[1100]; 12 scanf("%d %d %d",&n,&k,&m); 13 for(int i = 0; i < n; i++) 14 scanf("%lf",&p[i]); 15 16 f[0] = 0; 17 f[1] = p[0]; 18 for(int i = 2; i <= m; i++) 19 { 20 f[i] = 0; 21 for(int j = 0; j < n; j++) 22 f[i] += p[j] * pow(f[i-1],j); 23 } 24 printf("Case #%d: %.7lf ",item,pow(f[m],k)); 25 } 26 return 0; 27 }