一开始有K只兔子(不知道原题中是什么生物=。=),每只兔子能活一天并有pi的概率生出i只新兔子,求m天后所有兔子死光的概率。
概率递推,设D[m]为m天后兔子全死光的概率,则D[m] = D[m-1]^0*p0+D[m-1]^1*p[1]+...D[m-1]^i*pi..
1 #include <string.h> 2 #include <math.h> 3 #include <stdio.h> 4 int cas, n, m, k; 5 double p[1005], d[1005]; 6 double dp(int x){ 7 if (x == 0) return 0; 8 if (x == 1) return p[0]; 9 if (d[x] != -1) return d[x]; 10 d[x] = 0; 11 for (int i = 0; i < n; i++) 12 d[x] += p[i] * pow(dp(x-1), i); 13 return d[x]; 14 } 15 int main(){ 16 scanf("%d", &cas); 17 for (int ca = 1; ca <= cas; ca++) { 18 scanf("%d%d%d", &n, &k, &m); 19 for (int i = 0; i < n; i++) 20 scanf("%lf", &p[i]); 21 for (int i = 0; i <= m; i++) 22 d[i] = -1; 23 printf("Case #%d: %.7lf\n", ca, pow(dp(m), k)); 24 } 25 return 0; 26 }