UV-11021 tribles (recurrence + probability)

Source: Internet
Author: User

Description

Problem
Tribbles
Input:
Standard Input

Output:Standard output

Gravitation,N.
"The tendency of all bodies to approach one another with a strength
Proportion to the quantity of matter they contain -- the quantity
Matter they contain being ascertained by the strength of their tendency
To approach one another. This is a lovely and edifying authentication
How science, having made a proof B, makes B the proof of ."

Ambrose Bierce

You have a populationKTribbles. This participates species of tribbles live for exactly one day and then die. Just before death, a single tribble has the probabilityPiOf giving birthIMore tribbles. What is the probability that afterMGenerations, every tribble will be dead?

Input
The first line of input gives the number of cases,N.NTest Cases Follow. Each one starts with a line containingN(1 <=N<= 1000 ),K(0 <=K<= 1000) andM(0 <=M<= 1000). The nextNLines will give the probabilitiesP0,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 
                      

Problemsetter: Igor naverniouk, EPS

Special thanks: Joachim Wulff

Q: K of them have only poll, and each living day will die. Some new poll may be generated before the death. Specifically, the probability of I being generated is pi, given m, calculate the probability that all the pods will die after M days. Note that the total death is not counted in M days.

Idea: the death of each ball does not affect each other. Therefore, you only need to find the probability F (m) of the total death of only one ball in the first place and M days. The formula of the full probability includes:

F (I) = P0 + p1 * F (I-1) + p2 * F (I-1) ^ 2 + .... pn-1 * F (I-1) ^ n-1, where PJ * F (I-1) ^ J indicates that the poll produced J offspring and all of them died after I-1, note that the death of J offspring is independent of each other, and the probability of each death is F (I-1), so according to the multiplication formula, the probability of all death of J offspring is F (I-1) ^ J. Finally, calculate K. Only the ball is needed.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 1010;int n, k, m;double p[maxn], f[maxn];int main() {int t, cas = 1;scanf("%d", &t);while (t--) {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] * pow(f[i-1], j); }printf("Case #%d: %.7lf\n", cas++, pow(f[m], k));}return 0;}



UV-11021 tribles (recurrence + probability)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.