---
id: 776
title: "Educational DP Contest : I - Coins 题解"
slug: educational-dp-contest-i-coins
lang: zh
date: 2019-02-19T21:34:04
date_gmt: 2019-02-19T13:34:04
modified: 2019-02-19T21:34:04
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, DP, 概率]
original_url: https://kalorona.com/oi/educational-dp-contest-i-coins-题解/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body headings were <h1> in the original; demoted to level 2 so the page title is the only h1."
comments: []
---

## 主要思路

概率 DP，显然地。

我们怎么来统计呢？在存下概率之后，我们枚举轮数并且枚举硬币向上的情况，之后转移即可，代码里一目了然。

## 代码

```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;
}
```
