GravitationN.
"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, has 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 have the probability Pi of giving birth to I more tribbles.
What's the probability that after M generations, every tribble would be dead?
Input
The. RST 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 would give the
Probabilities P0, P1, ..., pn−1.
Output
For each test case, output one line containing ' case #x: ' followed by the answer, correct
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.3300000
Case #2:0.4781370
Case #3:0.6250000
Case #4:0.3164062
Test Instructions : give you k ball, a ball can live one day, when it died there will be a probability pi birth i small Ball, (0<=i<n) now ask you m after all the ball all the probability of death is how much
Solution: We define F[M] as the probability of a small ball dying in live m days, then the answer is F[m]^k
For f[i] = P0 + P1 * (f[i-1]^1) + P2 * (F[i-1] ^ 2) + ...
Recursion to get answers
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd; typedefLong Longll;Const intn=10000;intMain () {intT, CAS =1, N, M, K; DoubleP[n],f[n]; scanf ("%d",&T); while(t--) {scanf ("%d%d%d",&n,&k,&m); for(inti =0; I < n; i++) scanf ("%LF",&P[i]); f[0] =0; for(inti =1; I <= m; i++) {F[i]=0.0; for(intj =0; J < N; J + +) {F[i]+ = p[j] * POW (f[i-1],j); }} printf ("Case #%d:%.7f\n", cas++, pow (f[m],k)); } return 0;}Code
Uva-11021-tribles recurrence Probability