首页 > 代码库 > UVA11021 Tribles[离散概率 DP]
UVA11021 Tribles[离散概率 DP]
UVA - 11021 Tribles |
GRAVITATION, n. “The tendency of all bodies to approach one another with a strength proportion to the quantity of matter they contain – the quantity of matter they contain being ascertained by the strength of their tendency to approach one another. This is a lovely and edifying illustration of how science, having made A the proof of B, makes B the proof of A.”
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 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 line containingn(1≤n≤1000),k(0≤k≤1000)andm(0≤m≤1000). Thenextnlineswillgivethe 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
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.3300000Case #2: 0.4781370Case #3: 0.6250000Case #4: 0.3164062
每只麻球相互独立,求一只就可以了
f[i]表示i天后1只麻球及后代全死亡的概率
f[i]=p[0]+p[1]*f[i-1]+p[2]*f[i-1]^2+........
边界f[0]=0
//// main.cpp// uva11021//// Created by Candy on 26/10/2016.// Copyright © 2016 Candy. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N=1005;int T,n,m,k;double f[N],p[N];inline double fp(double a,int b){ double ans=1.0; for(;b;b>>=1,a*=a) if(b&1) ans*=a; return ans;}void dp(){ f[0]=0; for(int i=1;i<=m;i++){ f[i]=p[0]; for(int j=1;j<n;j++) f[i]+=p[j]*fp(f[i-1],j); }}int main(int argc, const char * argv[]) { scanf("%d",&T); for(int cas=1;cas<=T;cas++){ scanf("%d%d%d",&n,&k,&m); for(int i=0;i<n;i++) scanf("%lf",&p[i]); dp(); printf("Case #%d: %.7lf\n",cas,fp(f[m],k)); } return 0;}
UVA11021 Tribles[离散概率 DP]