这题要注意到每只麻球的后代是独立存活的,所以如果某只麻球在某种情况下死亡的概率是P,那么k只麻球全部死亡的概率是Pk
设f[x]=每只麻球在x天后全部死亡的概率
f[i]=P0+P1f(i-1)+P2f(i-1)2+……+Pn-1f(i-1)n-1
最后由于有k个麻球,ans = f[m]k
#include <cstdio> #include <cstring> #define db double using namespace std; const int maxn = 1005; int kase, n, k, m; db p[maxn], f[maxn]; db ksm(db a, int b) { db ans = 1, base = a; while (b) { if (b & 1) ans *= base; base *= base; b >>= 1; } return ans; } int main() { scanf("%d", &kase); for (int KASE = 1; KASE <= kase; KASE++) { 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++) { f[i] = 0; for (int j = 0; j < n; j++) f[i] += p[j] * ksm(f[i - 1], j); } printf("Case #%d: %.7f ", KASE, ksm(f[m], k)); } return 0; }