Hihocode 1506 coin Toss

Source: Internet
Author: User

Describe

Little hi has a magical coin. The probability that the positive upward is the pi when the coin is thrown in the first time is known.

Now little hi wants to know what the probability of the right M-positive upward is if the total is thrown n times.
Input

The first line contains two integers of N and M.

The second line contains n real P1, P2, ... Pn.

For 30% of data, 1 <= N <= 20

For 100% data, 1 <= n <=, 0 <= M <= N, 0 <= Pi <= 1
Output

The output line of a real number represents the probability of just m-times positive upward. Note that the end of the line needs to contain a newline character ' \ n '.

The output and standard answer errors are considered correct within 0.001.
Sample input

2 1
0.5 0.5

Sample output

0.500000

DP[I][J] means I cast, J Times face up.

 #include <iostream> #include <algorithm> #include <iostream> #include <
Cstdio> #include <cstring> using namespace std;
const int MAXN = 1E3+50;
Double A[MAXN], DP[MAXN][MAXN];
    int main () {int n,m;
        while (scanf ("%d%d", &n,&m)! = EOF) {for (int i=1;i<=n;i++) scanf ("%lf", a+i);
        Dp[0][0] = 1, dp[0][1] = 0;
            for (int i=1;i<=n;i++) {dp[i][0] = dp[i-1][0]* (1.0-a[i]);
            for (int j=1;j<=i;j++) {Dp[i][j] = dp[i-1][j]* (1.0-a[i]) + dp[i-1][j-1]*a[i];
    }} printf ("%lf\n", Dp[n][m]);
} return 0; }

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.