---
id: 776
lang: en
translation_of: index.md
source_sha256: a14cad00ca707b13bc67226ab358127980e5e26b2b0b006bb0c3bd3f90f11b38
title: "Educational DP Contest: I - Coins — Solution"
description: "Probability DP: after storing the probabilities, enumerate the rounds and the coins landing heads up, then just transition."
tags: ["OI","DP","Probability"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

Probability DP, obviously.

How do we count it? After storing the probabilities, we enumerate the rounds and enumerate the cases of coins landing heads up, then just transition. It's all clear at a glance in the code.

## Code

```cpp
// I.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3030;
int n;
double prob[MAX_N], dp[MAX_N][MAX_N];
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lf", &prob[i]);
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= i; j++)
        {
            int x = j, y = i - j;
            if (x)
                dp[x][y] += dp[x - 1][y] * prob[i];
            if (y)
                dp[x][y] += dp[x][y - 1] * (1 - prob[i]);
        }
    double ans = 0;
    for (int i = 0; n - i > i; i++)
        ans += dp[n - i][i];
    printf("%.10lf", ans);
    return 0;
}
```
